php unset 在数组的 foreach 中不起作用

php unset not working in foreach for array

服务器nginx+php-fpm php7.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;在循环中然后将设置好。

解决方案

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