I have a class like so:
stdClass Object
(
[id] => 1
[items] => stdClass Object
(
[0] => 123
[1] => 234
[2] => 345
[3] => 456
)
)
)
Let’s call the above object $foo.
Let’s say $v = 234. Given $foo and $v, how can I return the “key” 1?
If $foo->items was an array I would simply do $key = array_search($v,$foo->items);. But this doesn’t work in an object.
How can I find the key for $v without looping through the object in some foreach?
Use
get_object_varsand search through the array returned.Reference: http://php.net/manual/en/function.get-object-vars.php
Here’s an example of how to search through the array returned for a key:
Output:
CodePad: http://codepad.org/in4w94nG