How to find index of object in php array?

前端 未结 10 1908
时光说笑
时光说笑 2021-02-13 18:46

Here is print_r output of my array:

Array
(
[0] => stdClass Object
    (
        [itemId] => 560639000019
        [name] => Item no1
        [code] =>         


        
10条回答
  •  既然无缘
    2021-02-13 19:26

    $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.

提交回复
热议问题