SiD quakers
SiD quakers

Reputation: 63

php unset not working in foreach for array

Server nginx+php-fpm php 7.2

I tried 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);

But unset not working. If use $value = null; in cycle then will set fine.

Upvotes: 0

Views: 441

Answers (1)

Jimit H.
Jimit H.

Reputation: 195

Solution

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

Upvotes: 1

Related Questions