How to iterate through a nested stdClass? [duplicate]
Asked Answered
I

5

23

I have an object like this:

stdClass Object
(
    [_count] => 10
    [_start] => 0
    [_total] => 37
    [values] => Array
        (
            [0] => stdClass Object
                (
                    [_key] => 50180
                    [group] => stdClass Object
                        (
                            [id] => 50180
                            [name] => CriticalChain
                        )

                )

            [1] => stdClass Object
                (
                    [_key] => 2357895
                    [group] => stdClass Object
                        (
                            [id] => 2357895
                            [name] => Data Modeling
                        )

                )

            [2] => stdClass Object
                (
                    [_key] => 1992105
                    [group] => stdClass Object
                        (
                            [id] => 1992105
                            [name] => SQL Server Users in Israel
                        )

                )

            [3] => stdClass Object
                (
                    [_key] => 37988
                    [group] => stdClass Object
                        (
                            [id] => 37988
                            [name] => CDO/CIO/CTO Leadership Council
                        )

                )

            [4] => stdClass Object
                (
                    [_key] => 4024801
                    [group] => stdClass Object
                        (
                            [id] => 4024801
                            [name] => BiT-HR, BI & IT Placement Agency
                        )

                )

            [5] => stdClass Object
                (
                    [_key] => 37845
                    [group] => stdClass Object
                        (
                            [id] => 37845
                            [name] => Israel Technology Group
                        )

                )

            [6] => stdClass Object
                (
                    [_key] => 51464
                    [group] => stdClass Object
                        (
                            [id] => 51464
                            [name] => Israel DBA's
                        )

                )

            [7] => stdClass Object
                (
                    [_key] => 66097
                    [group] => stdClass Object
                        (
                            [id] => 66097
                            [name] => SQLDBA
                        )

                )

            [8] => stdClass Object
                (
                    [_key] => 4462353
                    [group] => stdClass Object
                        (
                            [id] => 4462353
                            [name] => Israel High-Tech Group
                        )

                )

            [9] => stdClass Object
                (
                    [_key] => 4203807
                    [group] => stdClass Object
                        (
                            [id] => 4203807
                            [name] => Microsoft Team Foundation Server
                        )

                )

        )

)

I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.

For example I tried this:

foreach ($res as $values) { print "\n"; print_r ($values); } 

It iterates trough the object, but it also gives me useless

10 0 37
Impetuous answered 29/4, 2013 at 20:37 Comment(6)
Will you show what you tried and explain how your attempt did not match your expectations?Giustino
sure. For example I tried this: foreach ($res as $values) { print "\n"; print_r ($values); } it iterates trough the object, but it also gives me useless 10 0 37Impetuous
foreach ($object->values as $arr) ....Whipcord
Thanks @adeneo, This got me further: foreach ($res->values as $values) { print "\n"; foreach ($values->group as $groups) { but I get stuck here. $groups is not an array. If I try to access it by index I get 5C 2D 1S 3C 4B 3I 5I 6S 4I 4M ...Impetuous
@MorDeror - posted some code, try that =Whipcord
Side note: It looks like this is the result of a json_decode($json_string) call. This function offers the option to return an associative array rather than a stdClass object by calling it as follows: json_decode($json_string, true). Using this, you can traverse your data structure by array access all the way, which an be easier in some cases. In your case, you'd go foreach ($res['values'] as $value) { print_r($value); }, which will actually print the array the way you expect it to. You can also access the id with $value['group']['id'].Cata
W
28
echo "<table>"

foreach ($object->values as $arr) {
    foreach ($arr as $obj) {
        $id   = $obj->group->id;
        $name = $obj->group->name;

        $html  = "<tr>";
        $html .=    "<td>Name : $name</td>";
        $html .=    "<td>Id   : $id</td>";
        $html .= "</tr>";
    }
}

echo "</table>";
Whipcord answered 29/4, 2013 at 21:13 Comment(1)
There you go, I was just guessing the structure of a table there, but if you got it working, that's the important part.Whipcord
S
13

Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:

You can iterate overa stdclass simply by using foreach:

$user = new \stdClass();
$user->flag = 'red';

foreach ($user as $key => $value) {
   // $key is `flag`
   // $value is `red`
}
Summand answered 14/9, 2020 at 9:32 Comment(0)
P
6
function objectToArray( $data ) 
{
    if ( is_object( $data ) ) 
        $d = get_object_vars( $data );
}

Convert the Object to array first like:

$results = objectToArray( $results );

and use

foreach( $results as result ){... ...}
Popcorn answered 3/8, 2014 at 4:23 Comment(1)
Where is the return statement?Loewe
F
4

I know it's an old post , but for sake of others: when working with stdClass you should use Reflections:

  $obj = new ReflectionObject($object);

  $propeties = $obj->getProperties();

      foreach($properties as $property) {
        $name = $property->getName();  <-- this is the reflection class
        $value = $object->$name;       <--- $object is your original $object

            here you need to handle the result (store in array etc)



        }
Frisket answered 14/9, 2019 at 20:21 Comment(1)
Worked like a charm for me. Take not that this sample uses an interpolated object property name $object->$name, which is set in the line above.Macmahon
E
3
foreach($res->values as $value) {
    print_r($value);
}
Elsi answered 29/4, 2013 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.