简体   繁体   中英

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

I'm having a hard time trying to update the values in my array. I made a simple example to illustrate this: the array contains names of players and the amount of points they have. After each round I want to update their points like this:

(which is 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 as $player) {
        if ($player["id"] == $i) { 
         $player["points"] = ${"points".$i}; 
     }    $i++;
 }

 var_dump($players);

Must be something stupid, but I've been trying for hours and I just can't find it.

Thanks for the help!

You need to add a reference to $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 as &$player) {
  if ($player["id"] == $i) {
    $player["points"] = ${"points".$i};
  }
  $i++;
}

The crucial part is the ampersand & in the foreach statement. Without it you are not recording any changes to the array.

Seems like you'd like to do that using a Loop. If that is the case, you might want to consider changing the $points Variable to an Array like the Code below demonstrates:

<?php


    $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
        ),

    );

    // IF YOU INTEND TO ASSIGN THE VALUES USING A LOOP, IT IS SUGGESTED TO
    // RATHER MAKE $points AN ARRAY WITH EACH KEY CORRESPONDING TO THE KEY OF THE
    // MULTIDIMENSIONAL ARRAY $players AND THE VALUE BEING THE POINT TO BE ASSIGNED
    // TO THE SUB-ARRAY WITH THAT KEY LIKE SO:
    $points = [
      0   => 10,//IMPLIES: TARGET $players[0] & ADD 10 TO ITS points ITEM: "John"
      1   => 20,//IMPLIES: TARGET $players[1] & ADD 20 TO ITS points ITEM: "Chris"
      2   => 30,//IMPLIES: TARGET $players[2] & ADD 30 TO ITS points ITEM: "Peter"
      3   => 40,//IMPLIES: TARGET $players[3] & ADD 40 TO ITS points ITEM: "Greg"
    ];

    // NOW LOOP THROUGH THE $players MULTIDIMENSIONAL ARRAY...
    // AS YOU ITERATE THROUGH IT, TRY TO OBTAIN THE POINTS FROM THE $points ARRAY
    // USING THE LOOP INDEX ($key)....
    // IT IS ALSO IMPORTANT TO WORK WITH EACH PLAYER BY REFERENCE
    // USING THE & OPERATOR
    foreach ($players as $key=>&$player) {  // NOTICE THE &$player here
        $currentPlayersPoints   = $points[$key];
        // HAVING OBTAINED THE CURRENT PLAYER'S POINTS,
        // SIMPLY ADD THE VALUE TO THE THE ORIGINAL points ELEMENT
        $player['points'] += (int)$currentPlayersPoints;
    }

    // CHECK OUT YOUR RESULT::
    var_dump($players);

    // YIELDS:  
    array (size=4)
      0 => 
        array (size=3)
          'id'      => int 0
          'name'    => string 'John' (length=4)
          'points'  => int 10
      1 => 
        array (size=3)
          'id'      => int 1
          'name'    => string 'Chris' (length=5)
          'points'  => int 20
      2 => 
        array (size=3)
          'id'      => int 2
          'name'    => string 'Peter' (length=5)
          'points'  => int 30
      3 => 
        array (size=3)
          'id'      => int 3
          'name'    => string 'Greg' (length=4)
          'points'  => int 40

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM