How to access stdclass object after a specific key value pair?
Asked Answered
C

7

20

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?

Chemise answered 3/5, 2011 at 21:13 Comment(5)
have you tried something along the lines of Object['text']['values'][0]['id'] That's pretty deep array ;)Wafture
The problem is it would return the object with text value "Named after", and I only want the ones with text value "Parent". Thanks!Chemise
in your loop do a check if(Object['text'] === 'Parent') echo 'Found parent'Wafture
I'm unsure what language you're using to iterate, but in PHP, it's $object->values[0]->id. Edit: Oh and "values" is not an object, but an object property.Celia
Also @Wafture how do I access the next value? Because the object is not Object['text']['values'], but Object['values']Chemise
W
-1

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.

Wafture answered 3/5, 2011 at 22:49 Comment(3)
The issue here being that I would get the Object['values'] that came after the one that is Object['text'] = Named after, which isn't what I want ;)Chemise
if you had used the checking condition up there as I mentioned earlier conjoined with this piece of code, you'd be able to find the array paired with the "Parent". I don't see any reason you can't do it if you were able to find the Parent with the echo statement.Wafture
Sorry if couldn't help, but thought id try. If you could find and get it to print when locating the parent, then all that's left is your syntax. But if that wasn't you who did the flags up on the checking condition, my mistake for making the assumption ;)Wafture
S
71

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
Selflove answered 26/2, 2013 at 16:41 Comment(2)
For the last option, that would be $object->attributes->{'0'}->idTequila
Error: Cannot use object of type stdClass as arrayDevries
M
2

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

echo get_object_vars($object)['values']['0']['id'];
Meso answered 15/2, 2014 at 22:38 Comment(1)
One thing to note: your "minimalized" version will only work for php version 5.4 or greater. That was the version that added function array dereferencing. If your php version is lower, Somwang's two-step process would be necessary.Amyamyas
A
2

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 !!!

Ash answered 2/3, 2016 at 7:56 Comment(0)
B
1


        $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;


Bend answered 31/5, 2013 at 4:53 Comment(0)
N
0

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

<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
Nullify answered 1/11, 2013 at 13:33 Comment(0)
K
0

this can help you accessing subarrays in php using codeigniter framework

foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
                                            echo $novo_puto_ultimos_30_dias;
Kaisership answered 2/9, 2021 at 8:16 Comment(1)
Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.Camelback
W
-1

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.

Wafture answered 3/5, 2011 at 22:49 Comment(3)
The issue here being that I would get the Object['values'] that came after the one that is Object['text'] = Named after, which isn't what I want ;)Chemise
if you had used the checking condition up there as I mentioned earlier conjoined with this piece of code, you'd be able to find the array paired with the "Parent". I don't see any reason you can't do it if you were able to find the Parent with the echo statement.Wafture
Sorry if couldn't help, but thought id try. If you could find and get it to print when locating the parent, then all that's left is your syntax. But if that wasn't you who did the flags up on the checking condition, my mistake for making the assumption ;)Wafture

© 2022 - 2024 — McMap. All rights reserved.