How to find index of object in php array?

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

    use array_search:

    $a = new stdClass;
    $b = new stdClass;
    $a->id = 1;
    $b->id = 2;
    
    $arr = array($a, $b);
    $index = array_search($b, $arr);
    
    echo $index;
    // prints out 1
    

提交回复
热议问题