检查和更改数组中的值

时间:2012-06-14 22:53:38

标签: php

我有一个存储在SESSION中的多数组

我遍历数据以更改设置为“1”的值并将其设置为“0”

但由于某些原因,即使它确实找到了价值,也不会改变它。

这就是我所拥有的:

Array (
    [0] => 1 
    [1] => 0 
    [2] => admin_user
    [8] => 1 
    [3] => 2
    [4] => Array (
        [7] => Array ( 
            [0] => User1
            [1] => 1 
            [2] => 1 
            [3] => w.jpg 
            [4] => 1 
            [5] => 1 
            [6] => 500 
            [7] => 50
            [8] => 3000 
        )
        [1] => Array ( 
            [0] => User2 
            [1] => 1 
            [2] => 3 
            [3] => d.jpg 
            [4] => 1
            [5] => 0 
            [6] => 200 
            [7] => 85 
            [8] => 5000
        ) 
    )
)

所以在PHP中,我试图在位置4的数组中更改位置5的值。

我就这样做了:

foreach($_SESSION['player_data'][4] as $key){
    if($key[5] == 1){
       $_SESSION['player_data'][4][$key][5] = 0;
       break;
    }
}

但它不会将它设置为0它仍然是1令人讨厌:(我哪里出错?

1 个答案:

答案 0 :(得分:4)

更改为:

foreach($_SESSION['player_data'][4] as $key => $value){    
   if($value[5] == 1){
       $_SESSION['player_data'][4][$key][5] = 0;
       break;
    }
}