php: how to update a value in associative array by key -


i'm having hard time trying update values in array. made simple example illustrate this: array contains names of players , amount of points have. after each round want update points this:

(which not working)

 $players = array (    array (    "id"        => 0,     "name"      => "john",    "points"    => 0    ),     array (    "id"        => 1,     "name"      => "chris",    "points"    => 0    ),     array (    "id"        => 2,     "name"      => "peter",    "points"    => 0    ),     array (    "id"        => 3,     "name"      => "greg",    "points"    => 0    ),   );    $points0 = 10;  $points1 = 20;  $points2 = 30;  $points3 = 40;    $i = 0;  foreach ($players $player) {         if ($player["id"] == $i) {           $player["points"] = ${"points".$i};       }    $i++;  }   var_dump($players); 

must stupid, i've been trying hours , can't find it.

thanks help!

you need add reference $player:

$players = array (   array (    "id"        => 0,    "name"      => "john",    "points"    => 0    ),     array (    "id"        => 1,    "name"      => "chris",    "points"    => 0    ),     array (    "id"        => 2,    "name"      => "peter",    "points"    => 0    ),     array (    "id"        => 3,    "name"      => "greg",    "points"    => 0    ),  );   $points0 = 10; $points1 = 20; $points2 = 30; $points3 = 40;   $i = 0; foreach ($players &$player) {   if ($player["id"] == $i) {     $player["points"] = ${"points".$i};   }   $i++; } 

the crucial part ampersand & in foreach statement. without not recording changes array.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -