2017-04-07 125 views
2

我很難嘗試更新數組中的值。我舉了一個簡單的例子來說明這個問題:數組包含玩家的名字和他們擁有的點數。每一輪之後,我想更新自己的點是這樣的: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 
    ), 

); 


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

必須是愚蠢的東西,但我一直在努力了幾個小時,我只是無法找到它。

感謝您的幫助!

+0

你調試你的代碼?檢查壓縮和賦值之間的區別[運營商](http://php.net/manual/en/language.operators.php) – hassan

+5

'$ player [「points」] == $ {「points」。$ i} ;'你在這裏比較,你想分配我相信'$ player [「points」] = $ {「points」。$ i};' - 編輯:* FIrst in * ;-) –

+0

你可能想要使用點數組。 '$ points [0] = 10; $ points [1] = 20;'等等然後你可以做foreach($ player作爲$ player){$ player ['points'] = $ points [$ player ['id 「]]; }' – mkaatman

回答

5

您需要添加一個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語句中的符號&。沒有它,你不記錄對陣列的任何改變。

+0

該死的......爲什麼呢?我不明白。謝謝! – Roddeh

+2

http://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference @Roddeh –

+0

(y)永遠不會想出這個......謝謝 – Roddeh

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 
+0

使感謝,謝謝! – Roddeh