PHP stdClass How to get an object within an array that's also in an array?
-
21-12-2019 - |
문제
Array
(
[data] => Array
(
[0] => stdClass Object
(
[name] => name
[id] => 123
)
)
I have something like this. I am trying to get the id from within an array of an array... which kinda confuses me.
To get the id: echo $array[0]->id;
But then how do I get that array from array[data]?
Thanks
해결책
If the first array is in a var named $array
.
Your array $array
contains an other array into the 'data'
key.
So to retrieve this second array you can do this :
$second_array = $array['data']; //this will return the second array
$second_array
contains an object into the 0
key. You can retrieve this object, as you said with :
$obj = $second_array[0];
And then to retrieve the id
from $obj
$obj->id;
To get the id
in one line :
$array['data'][0]->id; //will return the attr id from the object
제휴하지 않습니다 StackOverflow