3

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!

Roddeh
  • 207
  • 1
  • 6
  • 19
  • have you debugged your code? check out the difference between compression and assignment [operators](http://php.net/manual/en/language.operators.php) – hassan Apr 07 '17 at 14:06
  • 5
    `$player["points"] == ${"points".$i};` you're comparing here, you want to assign I do believe `$player["points"] = ${"points".$i};` - Edit: *FIrst in* ;-) – Funk Forty Niner Apr 07 '17 at 14:07
  • You might want to use an array for points. `$points[0] = 10;$points[1]=20;` etc. Then you can do `foreach($players as $player){ $player['points'] = $points[$player['id']]; }` – Matt Apr 07 '17 at 14:10
  • @Fred-ii- What surrounding `{}` means in this case (variable)? Is that called somehow so i could read it? – Antonios Tsimourtos Apr 07 '17 at 14:10
  • @AntonisTsimourtos variables variable http://php.net/manual/en/language.variables.variable.php – Funk Forty Niner Apr 07 '17 at 14:11
  • yeah sorry, ofcourse that should be $player["points"] = ${"points".$i}; Does not work either... – Roddeh Apr 07 '17 at 14:14

2 Answers2

8

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.

glaux
  • 701
  • 7
  • 20
2

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
Poiz
  • 7,611
  • 2
  • 15
  • 17