How to find index of object in php array?

前端 未结 10 1875
时光说笑
时光说笑 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:16

    A funny alternative

    $getIdUnset = function($id) use ($myArray)
    {
        foreach($myArray as $key => $obj) {
            if ($obj->id == $id) {
                return $key;
            }
        }
    
        return false;
    };
    
    if ($unset = $getIdUnset(4)) {
        unset($myArray[$unset]);
    }
    

提交回复
热议问题