How to find index of object in php array?

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

    foreach($parentObj AS $key=>$element){
      if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
        echo "Gottcha! The index is - ". $key;
      }
    }
    

    $parentObj is obviously your root array - the one that holds all the others.

    We use the foreach loop to iterate over each item and then test it's id property against what ever value you desire. Once we have that - the $key that we are on is the index you are looking for.

提交回复
热议问题