oop – stdClass Object foreach PHP – Education Career Blog

Been searching for a solution for my problem. Seams meny has the same q as me, but still haven’t got a solution for my problem.

I have a stdClass Object that needs to be printed out in a foreach or somlike like that.

Here is a cut of the result i get with a “print_r($result)”.

stdClass Object
(
    ServiceGroup => Array
        (
            0 => stdClass Object
                (
                    Service => Array
                        (
                            0 => stdClass Object
                                (
                                    _ => 3D-modulering
                                    count => 71
                                )

                            1 => stdClass Object
                                (
                                    _ => CAD
                                    count => 81
                                )

                            2 => stdClass Object
                                (
                                    _ => Databasutveckling
                                    count => 118
                                )

                            3 => stdClass Object
                                (
                                    _ => Datainmatare
                                    count => 6
                                )

                            4 => stdClass Object
                                (
                                    _ => Driftsteknik
                                    count => 87
                                )

                            5 => stdClass Object
                                (
                                    _ => IT-konsult
                                    count => 39
                                )

                            6 => stdClass Object
                                (
                                    _ => IT-tekniker
                                    count => 223
                                )

                            7 => stdClass Object
                                (
                                    _ => Mjuk- och hårdvarutestning
                                    count => 150
                                )

                            8 => stdClass Object
                                (
                                    _ => Nätverksteknik
                                    count => 142
                                )

                            9 => stdClass Object
                                (
                                    _ => Produkttestare
                                    count => 171
                                )

                            10 => stdClass Object
                                (
                                    _ => Programmerare
                                    count => 146
                                )

                            11 => stdClass Object
                                (
                                    _ => Projektledning
                                    count => 156
                                )

                            12 => stdClass Object
                                (
                                    _ => Serviceteknik
                                    count => 157
                                )

                            13 => stdClass Object
                                (
                                    _ => Support
                                    count => 360
                                )

                            14 => stdClass Object
                                (
                                    _ => Systemadministration
                                    count => 145
                                )

                            15 => stdClass Object
                                (
                                    _ => Systemutveckling
                                    count => 110
                                )

                            16 => stdClass Object
                                (
                                    _ => Webbdesign
                                    count => 246
                                )

                            17 => stdClass Object
                                (
                                    _ => Webbutveckling
                                    count => 135
                                )

                            18 => stdClass Object
                                (
                                    _ => webmaster
                                    count => 103
                                )

                        )

                    name => Data/IT
                )

            1 => stdClass Object
                (
                    Service => Array
                        (
                            0 => stdClass Object
                                (
                                    _ => Affärsanalys
                                    count => 192
                                )

                            1 => stdClass Object
                                (
                                    _ => Aktuarie
                                    count => 20
                                )

,

I don’t know exactly what you want, but can easily foreach through objects just like arrays. This example shows that.

foreach($result->ServiceGroup as $value){
    foreach($value->Service as $obj){
        echo $obj->_;
        echo $obj->count;
    }
    echo $value->name;
}

Leave a Comment