1

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.

3
  • The problem here is that your function does not have access to $teams and $players. Try using var_dump to echo out the contents of those two arrays in your addPlayer function. Also please turn error_reporting on to report E_NOTICEs as that would have showed you the issues. cHao was trying to tell you that objects would be better suited for this sort of behaviour, and I totally agree! You should look into objects for this. Commented May 8, 2014 at 4:06
  • Thank you for your help.... I went with arrays because I am going to be pulling data temporarily from the database into these arrays and then spitting them back into the database. Would objects still be more useful? Commented May 8, 2014 at 4:13
  • @Nash: They might be. The logic and code would end up cleaner, at least...and you'd be more likely to get warnings and errors when you're using nonexistent variables. (I know that sounds bad, but it's a good thing. If your PHP were displaying notices, that could have clued you in on the scope issues.) Commented May 8, 2014 at 4:29

1 Answer 1

2
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).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.