简体   繁体   English

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

[英]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 : 你需要添加对$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. 关键部分是&符号& foreach声明。 Without it you are not recording any changes to the array. 没有它,您不会记录对阵列的任何更改。

Seems like you'd like to do that using a Loop. 好像你想用Loop做这件事。 If that is the case, you might want to consider changing the $points Variable to an Array like the Code below demonstrates: 如果是这种情况,您可能需要考虑将$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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