How to delete object from array inside foreach loop?

PhpForeachUnsetArrays

Php Problem Overview


I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work.

foreach($array as $element) {
    foreach($element as $key => $value) {
        if($key == 'id' && $value == 'searched_value'){
            //delete this particular object from the $array
            unset($element);//this doesn't work
            unset($array,$element);//neither does this
        } 
    }
}

Any suggestions. Thanks.

Php Solutions


Solution 1 - Php

foreach($array as $elementKey => $element) {
    foreach($element as $valueKey => $value) {
        if($valueKey == 'id' && $value == 'searched_value'){
            //delete this particular object from the $array
            unset($array[$elementKey]);
        } 
    }
}

Solution 2 - Php

Be careful with the main answer.

with

[['id'=>1,'cat'=>'vip']
,['id'=>2,'cat'=>'vip']
,['id'=>3,'cat'=>'normal']

and calling the function

foreach($array as $elementKey => $element) {
    foreach($element as $valueKey => $value) {
        if($valueKey == 'cat' && $value == 'vip'){
            //delete this particular object from the $array
            unset($array[$elementKey]);
        } 
    }
}

it returns

[2=>['id'=>3,'cat'=>'normal']

instead of

[0=>['id'=>3,'cat'=>'normal']

It is because unset does not re-index the array.

It reindexes. (if we need it)

$result=[];
foreach($array as $elementKey => $element) {
    foreach($element as $valueKey => $value) {
        $found=false;
        if($valueKey === 'cat' && $value === 'vip'){
            $found=true;
            $break;
        } 
        if(!$found) {
           $result[]=$element;
        }
    }
}

Solution 3 - Php

It looks like your syntax for unset is invalid, and the lack of reindexing might cause trouble in the future. See: [the section on PHP arrays][1].

The correct syntax is shown above. Also keep in mind [array-values][2] for reindexing, so you don't ever index something you previously deleted.

[1]: http://php.net/manual/en/language.types.array.php "PHP arrays." [2]: http://www.php.net/manual/en/function.array-values.php "reindexing arrays"

Solution 4 - Php

This should do the trick.....

reset($array);
while (list($elementKey, $element) = each($array)) {
    while (list($key, $value2) = each($element)) {
        if($key == 'id' && $value == 'searched_value') {
            unset($array[$elementKey]);
        }
    }
}

Solution 5 - Php

You can also use references on foreach values:

foreach($array as $elementKey => &$element) {
    // $element is the same than &$array[$elementKey]
    if (isset($element['id']) and $element['id'] == 'searched_value') {
        unset($element);
    }
}

Solution 6 - Php

I'm not much of a php programmer, but I can say that in C# you cannot modify an array while iterating through it. You may want to try using your foreach loop to identify the index of the element, or elements to remove, then delete the elements after the loop.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionababaView Question on Stackoverflow
Solution 1 - PhpprodigitalsonView Answer on Stackoverflow
Solution 2 - PhpmagallanesView Answer on Stackoverflow
Solution 3 - Phppablo.meierView Answer on Stackoverflow
Solution 4 - PhpJoshView Answer on Stackoverflow
Solution 5 - Phpair-dexView Answer on Stackoverflow
Solution 6 - PhpCorey SunwoldView Answer on Stackoverflow