PHP未设置无法在foreach数组中工作

时间:2019-07-18 11:03:14

标签: php foreach unset

服务器nginx + php-fpm php 7.2

我尝试了http://sandbox.onlinephpfunctions.com/code/200d19b2663ee01391b9d0a1745ab677b3f219df

$accounts = [
    0 => [
        "active" => true    
    ],
    1 => [
        "active" => false    
    ]
];

foreach($accounts as &$value) {

    if($value['active'] === false) {
         var_dump($value);
         unset($value);
    }

}
unset($value);

print_r($accounts);

但未设置无效。如果使用$ value = null;然后再进行设置即可。

1 个答案:

答案 0 :(得分:1)

解决方案

$accounts = [
        0 => [
            "active" => true    
        ],
        1 => [
            "active" => false    
        ]
];

foreach($accounts as $index=>$value) {

    if($value['active'] === false) {
        var_dump($value);
        unset($accounts[$index]);
    }

}
//unset($value);

print_r($accounts);