我在尝试更新数组中的值时遇到了困难。我制作了一个简单的示例来说明这一点:数组包含球员的姓名和他们拥有的积分数量。每轮比赛后,我想像这样更新他们的积分:
(但这并没有起作用)
$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);
可能很愚蠢,但我已经尝试了几个小时,但我就是找不到。
感谢您的帮助!
$player["points"] = ${"points".$i};
。编辑提示:首先。 - Funk Forty Niner$points[0] = 10;$points[1]=20;
等等。然后,你可以使用foreach($players as $player){ $player['points'] = $points[$player['id']]; }
来为每个玩家分配对应的分数。 - Matt{}
表示什么(变量)?我能以某种方式读取它吗? - Antonios Tsimourtos