How to find object in php array and delete it?

arrays, php

Solution

$found = false;  
foreach($values as $key => $value) {
    if ($value->id == 4) {
        $found = true;
        break;
    }
}

if ($found) unset($values[$key]);

This is considered to be faster then any other solution since we only iterate the `array` to until we find the object we want to remove.

Note: You should not remove an element of an array while iterating so we do it afterwards here.

Problem

Here is print_r output of my array: ``` Array ( [0] => stdClass Object ( [itemId] => 560639000019 [name] => Item no1 [code] => 00001 [qty] => 5 [id] => 2 ) [1] => stdClass Object ( [itemId] => 470639763471 [name] => Second item [code] => 76347 [qty] => 9 [id] => 4 ) [2] => stdClass Object ( [itemId] => 56939399632 [name] => Item no 3 [code] => 39963 [qty] => 6 [id] => 7 ) ) ``` How can I find index of object with [id] => 4 in order to remove it from array?

Original source

Related problems