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

Posted

技术标签:

【中文标题】php:如何通过键更新关联数组中的值【英文标题】:php: how to update a value in associative array by key 【发布时间】:2017-09-02 23:01:25 【问题描述】:

我很难尝试更新数组中的值。我做了一个简单的例子来说明这一点:数组包含玩家的姓名和他们拥有的点数。在每一轮之后,我想像这样更新他们的积分:

(不工作)

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

一定是一些愚蠢的东西,但我已经尝试了几个小时,但还是找不到。

感谢您的帮助!

【问题讨论】:

你调试过你的代码吗?查看压缩和赋值的区别operators $player["points"] == $"points".$i; 你在这里比较,你想分配我相信$player["points"] = $"points".$i; - 编辑:FIrst in ;-) 您可能希望使用数组来表示点。 $points[0] = 10;$points[1]=20; 等然后你可以做foreach($players as $player) $player['points'] = $points[$player['id']]; @Fred-ii- 在这种情况下, 周围的含义是什么(变量)?是以某种方式调用的,所以我可以阅读它吗? @AntonisTsimourtos 变量变量php.net/manual/en/language.variables.variable.php 【参考方案1】:

您似乎想使用循环来执行此操作。如果是这种情况,您可能需要考虑将 $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

【讨论】:

【参考方案2】:

您需要将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 as &$player) 
  if ($player["id"] == $i) 
    $player["points"] = $"points".$i;
  
  $i++;

关键部分是foreach 语句中的& 符号&amp;。没有它,您将不会记录对数组的任何更改。

【讨论】:

该死的……这是为什么呢?我不明白。谢谢! ***.com/questions/2030906/…@Roddeh

以上是关于php:如何通过键更新关联数组中的值的主要内容,如果未能解决你的问题,请参考以下文章

PHP 数组中的每个ID键关联一个值,可不可以关联两个值?

php 数组索引值如何重新从0开始递增

PHP关联数组重复键

当字符串包含 PHP 中的重复键时,如何将字符串转换为关联数组? [复制]

php - 如何在关联数组的数组中搜索多个键/值对并返回匹配项?

php:如何从数字索引中获取关联数组键?