As you showed in your example, you are dealing with stdClass objects. They are very similar to arrays and with PHP you can easily convert between the two with what is called casting:
$object = $foo->items; $key = array_search($v, (array)$object); ^^^^^^^--- cast to array
As shown in this small example (I just used the $object variable to make the translation more visible, you can usually write it as single-line), casting from object to array allows you to use the well-known function ( array_search ) on the object.
Since arrays and stdClass objects in PHP are so similar, this works both ways:
$array = ['property' => 'value']; $object = (object)$array; echo $object->property;
This also works with other types in PHP, so there is probably something to read beyond your specific problem: Juggling & shy; Docs , but be careful that in PHP this has many special rules. But between the array and the objects, it is pretty straightforward.
hakre source share