I am currently trying to create a team generator for a game that organizes teams based on player ratings. I am having a little issue when it comes to adding players in a nested array. I will eventually be adding database calls to the arrays. I can't figure out why I can't echo the players after I try and add them to the teams array. The randoms are for testing purposes.
$players = array();
$captains = array();
for ($i = 1; $i <= 40; $i++){
$players[] = array('name' => 'Player ' . $i, 'MMR' => rand(2800,4200));
}
for ($i = 1; $i <= 10; $i++){
$captains[] = array('name' => 'Captain ' . $i, 'MMR' => rand(3200,4200));
}
//sort the players by MMR
usort($players, function($a, $b) {
return $a['MMR'] - $b['MMR'];
});
//sort the captains by MMR
usort($captains, function($a, $b) {
return $a['MMR'] - $b['MMR'];
});
//put captains on teams
$teams = array();
for($i = 0;$i < count($captains); $i++){
$teams[] = array('name' => 'Team ' . ($i + 1), 'captain' => $captains[$i], 'players' => array(), 'totalMMR' => $captains[$i]['MMR']);
}
Here is where I think the problem may be:
function addPlayer($team,$newPlayer){
$teams[$team]['players'][] = $players[$newPlayer];
$teams[$team]['totalMMR'] += $players[$newPlayer]['MMR'];
}
addPlayer(0,0);
$output = '';
foreach($teams as $team){
$output .= '<div class="teams">' . $team['name'] . '<br />' . $team['captain']['name'] . ': ' . $team['captain']['MMR'] . '<br />';
for ($i = 0; $i < count($team['players']); $i++){
$output .= $team['players'][$i]['name'] . ': ' . $team['players'][$i]['MMR'] . '<br />';
}
$output .= '</div>';
}
echo $output;
Now the captains are echoing out, but the player that I added is not. Any help would be appreciated.
function addPlayer($team,$newPlayer){
$teams[$team]['players'][] = $players[$newPlayer];
$teams[$team]['totalMMR'] += $players[$newPlayer]['MMR'];
}
There's no variable named $teams in this function. If you mean to modify a global variable named $teams, then you can say global $teams; as the first line in your function.
Likewise for $players (although you should have gotten a notice about undefined indexes).
Related
i have a set of arrays:
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
what i want to do is to get a set of $data array from each number of $nums array,
the output must be:
output:
2=11,22
3=33,44,55
1=66
what i tried so far is to slice the array and remove the sliced values from an array but i didn't get the correct output.
for ($i=0; $i < count($nums); $i++) {
$a = array_slice($data,0,$nums[$i]);
for ($x=0; $x < $nums[$i]; $x++) {
unset($data[0]);
}
}
Another alternative is to use another flavor array_splice, it basically takes the array based on the offset that you inputted. It already takes care of the unsetting part since it already removes the portion that you selected.
$out = array();
foreach ($nums as $n) {
$remove = array_splice($data, 0, $n);
$out[] = $remove;
echo $n . '=' . implode(',', $remove), "\n";
}
// since nums has 2, 3, 1, what it does is, each iteration, take 2, take 3, take 1
Sample Output
Also you could do an alternative and have no function usage at all. You'd need another loop though, just save / record the last index so that you know where to start the next num extraction:
$last = 0; // recorder
$cnt = count($data);
for ($i = 0; $i < count($nums); $i++) {
$n = $nums[$i];
echo $n . '=';
for ($h = 0; $h < $n; $h++) {
echo $data[$last] . ', ';
$last++;
}
echo "\n";
}
You can array_shift to remove the first element.
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
foreach( $nums as $num ){
$t = array();
for ( $x = $num; $x>0; $x-- ) $t[] = array_shift($data);
echo $num . " = " . implode(",",$t) . "<br />";
}
This will result to:
2 = 11,22
3 = 33,44,55
1 = 66
This is the easiest and the simplest way,
<?php
$nums = array(2,3,1);
$data = array(11,22,33,44,55,66);
$startingPoint = 0;
echo "output:"."\n";
foreach($nums as $num){
$sliced_array = array_slice($data, $startingPoint, $num);
$startingPoint = $num;
echo $num."=".implode(",", $sliced_array)."\n";
}
?>
I want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track of what number person im on. -- im using PHP
<?php
$name_array = array('pon', 'zi', 'pol', 'et');
function name_person($name, $number) {
echo $name . ' is person #' . $number . ' ';
}
for ($i = 0; $i <= 4; $i++) {
$counter = 1;
while ($counter <=4) {
name_person( $name_array[$i], $counter);
$counter++;
}
}
my output should be:
pon is person #1, zi is person #2, pol is person #3, et is person #4
Can you please help me?
you can use array_walk to apply a user supplied function to every member of an array.
Try this code.
$name_array = array('pon', 'zi', 'pol', 'et');
//remove 0 index from array (optional)
$name_array = array_filter(array_merge(array(0), $name_array));
function name_person($name, $number) {
echo $name . ' is person #' . $number . ' ';
}
//apply a user supplied function to every member of an array
array_walk($name_array, 'name_person');
Output:
pon is person #1 zi is person #2 pol is person #3 et is person #4
Try this:
<?php
$name_array = array('pon', 'zi', 'pol', 'et');
function name_person($name, $number) {
echo $name . ' is person #' . $number . ' ';
}
for ($i = 0; $i < 4; $i++) {
name_person( $name_array[$i], $i + 1);
}
for exact output try this:
$name_array = array('pon', 'zi', 'pol', 'et');
function name_person($name, $number) {
echo $name . ' is person #' . $number;
}
foreach($name_array as $k=>$v) {
name_person( $v, $k+1).(($k==sizeof($name_array)-1)?'':', ');
}
There is a nested while loop within for loop.
for loop should run from 0 to <4 instead of <=4.
Lose the $counter variable and just use $i+1 instead.
So it should be:
for ($i = 0; $i < 4; $i++) {
name_person( $name_array[$i], $i+1);
}
I have this array :
$products = array('0', '1', '2', '3');
and I want to have this combination :
0
0,1
0,1,2
0,1,2,3 // my code can't show this result
0,1,3 // my code can't show this result
0,2
0,2,3
0,3
1
1,2
1,2,3
1,3
2
2,3
3
and here's my code :
$rows = count($products);
for ($i = 0; $i < $rows; $i++) {
echo $i . '<br>';
for ($j = $i + 1; $j < $rows; $j++){
echo $i . $j . '<br>';
if (!empty($products[$j+1])) {
echo $i . $j . $products[$j+1] . '<br>';
}
}
}
but, unfortunately I missed 2 results :
0,1,2,3
0,1,3
and I still have doubt about this for loop inside loop, while the number of combination is determined by array length.
how to get that missed combination and perfectly works for different array length?
$products = array('0', '1', '2', '3');
$rows = count($products); //missing here array count......
for ($i = 0; $i < $rows; $i++) {
echo $i . '<br>';
for ($j = $i + 1; $j < $rows; $j++){
echo $i . $j . '<br>';
if (!empty($products[$j+1])) {
echo $i . $j . $products[$j+1] . '<br>';
}
}
}
Try this
<?php
$set = array('0', '1', '2','3');
$power_set = possible_set($set);
echo "<pre>";
print_r($power_set);
echo "</pre>";
function possible_set($array) {
$results = array(array( ));
foreach ($array as $element)
foreach ($results as $inner_element)
array_push($results, array_merge(array($element), $inner_element));
unset($results[0]);
$results = array_values($results);
return $results;
}
?>
https://eval.in/516963
This is a very simple query and I must be doing something wrong, but am not seeing it right now. I'm using codebird php library to connect and search Twitter. I am able to find all friends/followers, create and destroy friendships, but for some reason am not paginating through a search of users correctly.
$cb is already connected and logged into twitter.
The problem that I'm having is that $u has the same list of users for each page. In the end my $users array will have multiple copies of the first page for each loop and I cannot get more than 20 users from this search. What is wrong with the query?
$count = 20; //max for users_search
$type = 'q';
$total = 0;
for ($i = 1; $i <= 2; $i++) {
echo 'Page ' . $i . "\r\n";
$u = $cb->users_search(array($type => $search, 'page' => $i, 'count' => $count));
echo var_dump($u);
$users[] = (array)$u;
}
Try this:
$count = 20; //max for users_search
$nbPage = 2; //2 pages for eg.
$type = 'q';
$search = '...';
for ($i = 1; $i <= $nbPage; $i++) {
$params = array($type => $search, 'page' => $i, 'count' => $count);
$data = (array) $cb->users_search($params);
for ($j = 0; $j < $count; $j++) {
$status = $data[$j];
echo $status->name . " (#" . $status->screen_name . ")", '<br>';
}
}
I have destinations array as array('destination1', 'destination2', 'destination3', 'destination4'). I want distinct combinations as
'destination1' => 'destination2'
'destination1' => 'destination3'
'destination1' => 'destination4'
'destination2' => 'destination3'
'destination2' => 'destination4'
what is the simple solution for doing this?
Did you want to do like this
$arr = array('destination1', 'destination2', 'destination3', 'destination4');
$limit = count($arr);
for($i = 0; $i < ($limit - 2); $i++){
for($j = $i + 1; $j < $limit; $j++){
echo $arr[$i].'=>'.$arr[$j].'</br>';
}
}
Improved and optimized version for any size:
while ($curr = array_shift($arr)) foreach ($arr as $item) echo $curr . '=>' . $item . '<br />';