简体   繁体   English

PHP stdClass如何在数组中也同时在数组中获取对象?

[英]PHP stdClass How to get an object within an array that's also in an array?

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. 我正在尝试从数组的数组中获取ID ...这让我感到困惑。

To get the id: echo $array[0]->id;

But then how do I get that array from array[data]? 但是,如何从array [data]获取该数组?

Thanks 谢谢

If the first array is in a var named $array . 如果第一个数组在名为$array的var中。

Your array $array contains an other array into the 'data' key. 您的数组$array'data'键中包含另一个数组。

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. $second_array包含一个放入0键的对象。 You can retrieve this object, as you said with : 您可以检索该对象,如您所说:

$obj = $second_array[0];

And then to retrieve the id from $obj 然后从$obj检索id

$obj->id;

To get the id in one line : 要在一行中获取id

$array['data'][0]->id; //will return the attr id from the object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM