Basic question about building data structure

I have a PHP question…

I am trying to build a data structure to contain a data set that I use in many places.

I am building the array to look like this





        $report =

        [

            'students' =>

            [

                [

                    'name' => 'Amy Smith',

                    'sessions' =>

                        [

                            [

                                'id' => '12',

                                'name' => 'Goo and Gunk',

                                'cost' => '114.00',

                                'products' =>

                                    [

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ],

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ]

                                    ]

                            ],

                            [

                                'id' => '587',

                                'name' => 'Rocket Fun',

                                'cost' => '114.00',

                                'products' =>

                                    [

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ],

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ]

                                    ]

                            ],

                        ]

                ], // end student

                [

                    'name' => 'Paul Jones',

                    'sessions' =>

                        [

                            [

                                'id' => '12',

                                'name' => 'Stuff and Fluff',

                                'cost' => '114.00',

                                'products' =>

                                    [

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ],

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ]

                                    ]

                            ],

                            [

                                'id' => '587',

                                'name' => 'Rocket Fun',

                                'cost' => '114.00',

                                'products' =>

                                    [

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ],

                                        [

                                            'id' => '587',

                                            'name' => 'Early Care 7:30 - 8:30',

                                            'cost' => '45.00'

                                        ]

                                    ]

                            ],

                        ]

                ], // end student

            ],

            'total' => '1010',

        ];




And I am trying to access the data something like this…





            $cart = \app\models\Carts::Getcart( \Yii::$app->user->identity->id );

            foreach( $cart as $student )

            {

                echo $student['name'];              <<-- Undefined index: name

                foreach( $student['session'] as $session )

                {


                   echo $session['name'];

                   foreach( $student['session'] as $session )

                   {

                      echo $session['name'];

                   }


                }

            }

            echo $cart['total'];



I am getting an Undefined index: name in the line

echo $student[‘name’];

I think I am going in the right direction and just don’t know the correct syntax.

I am still trying to wrap my brain around arrays and objects, and have not found

a good source that talks about complex data structures

It boils down to my not being sure how to iterate and extract data from this structure.

Thanks

-John

To debug this issue you could echo the $cart array to see if the structure is correct:




        	$cart = \app\models\Carts::Getcart( \Yii::$app->user->identity->id );

        	CVarDumper::dump($cart);

        	foreach( $cart as $student )



or even log it:




        	$cart = \app\models\Carts::Getcart( \Yii::$app->user->identity->id );

        	Yii::log(CVarDumper::dumpAsString($cart));

        	foreach( $cart as $student )



P.S. I’m using Yii 1.1 syntax.

You would have to loop over the objects in student. Try


$student[$i]['name']