How to access stdclass object after a specific key

2019-01-18 04:43发布

I have a stdclass object as shown below:

stdClass Object
(     
    [text] => Parent
    [values] => Array
        (
            [0] => stdClass Object
                (
                    [id] => /m/0c02911
                    [text] => Laurence W. Lane Jr.
                    [url] => http://www.freebase.com/view/m/0c02911
                )

        )

)

I iterate over multiple such objects, some of which have

stdClass Object
(
    [text] => Named after
    [values] => Array
        (
            [0] => stdClass Object
                (
                    [id] => /m/0c02911
                    [text] => Stanford
                    [url] => SomeURL
                )

        )

)

I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?

6条回答
Rolldiameter
2楼-- · 2019-01-18 05:19

Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:

echo get_object_vars($object)['values']['0']['id'];
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-18 05:21

there are serveral ways to turn it to array:

First Solution:

$value = get_object_vars($object);

Second Solution:

$value = (array) $object;

Third Solution

$value = json_decode(json_encode($object), true);

to get value of converted array

echo $value['values']['0']['id'];

The alternate way to access objects var without convert the object, try

$object->values->{'0'}->id
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-18 05:23

I'm doing the same thing and all I did was this;

<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
查看更多
Animai°情兽
5楼-- · 2019-01-18 05:31


        $Obj=stdClass Object
    (
        [text] => Named after
        [values] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => /m/0c02911
                        [text] => Stanford
                        [url] => SomeURL
                    )

            )

    )
    $Values= $result->values;
    $Item = $Values[0];
    $id=$Item->id;
    $text = $Item->text;
    $url=$Item->url;


查看更多
迷人小祖宗
6楼-- · 2019-01-18 05:35

I had the same issue, still not so sure why but I was able to get it working using this workaround:

$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2;  //****Not Working
$tmp  = (object)$elements;
$tmp = $tmp ->id;          //****Working
//$tmp =$elements['id'] ;  //****Not Working
return $tmp == $k2;

I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).

$elements can be Array to but I chose to demonstrate with json string.

I hope this helps somehow !!!

查看更多
Root(大扎)
7楼-- · 2019-01-18 05:37

What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like

Object['values'][0]['id']

or

Object['values'][0]->id

which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.

查看更多
登录 后发表回答