문제

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top