php:如何按键更新关联数组中的值

时间:2017-04-07 14:05:26

标签: php arrays associative-array

我很难尝试更新数组中的值。我做了一个简单的例子来说明这一点:数组包含了玩家的名字和他们拥有的点数。每轮结束后,我想更新他们的观点:

(这不起作用)

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

一定是蠢事,但我已经尝试了好几个小时而我却找不到它。

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

您需要向$player添加reference

$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++;
}

关键部分是&语句中的&符号foreach。如果没有它,您不会记录对阵列的任何更改。

答案 1 :(得分:2)

好像你喜欢用Loop做这件事。如果是这种情况,您可能需要考虑将$points变量更改为数组,如下面的代码所示:

<?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