Stack Exchange
Loop through this weird array in php
How do i loop through this array. I've tried the foreach statement with wierd results.   I need to access the values for StopName and Time. 
Thanks in advance
  Array
(
    [Direction0] => Array
        (
            [0] => Array
                (
                    [DirectionNum] => 0
                    [EndTime] => 2012-11-26T06:16:00
                    [RouteID] => 87v1
                    [StartTime] => 2012-11-26T05:51:00
                    [StopTimes] => Array
                        (
                            [0] => Array
                                (
                                    [StopID] => 3002573
                                    [StopName] => GREENBELT STATION + BUS BAY C
                                    [StopSeq] => 1
                                    [Time] => 2012-11-26T05:51:00
                                )

                            [1] => Array
                                (
                                    [StopID] => 3002579
                                    [StopName] => GREENBELT METRO DRIVE + CHERRYWOOD
                                    [StopSeq] => 6
                                    [Time] => 2012-11-26T05:52:26
                                )

                            [2] => Array
                                (
                                    [StopID] => 3002409
                                    [StopName] => CHERRYWOOD LA + IVY LA
                                    [StopSeq] => 8
                                    [Time] => 2012-11-26T05:53:31
                                )

                            [3] => Array
                                (
                                    [StopID] => 3002413
                                    [StopName] => CHERRYWOOD LA + US COURT HOUSE
                                    [StopSeq] => 10
                                    [Time] => 2012-11-26T05:54:00
                                )

                            [4] => Array
                                (
                                    [StopID] => 3002910
                                    [StopName] => POWDER MILL RD + #161
                                    [StopSeq] => 20
                                    [Time] => 2012-11-26T05:57:35
                                )

This code was originally in the form of a json object and i used the php json_decode function which translated this data into a multi-demensional array. Hope my question is clear enough to provide insight or solution. 

if the array is exactly as you post you can use foreach($array['Direction0'][0]['Stoptimes'] as $stoptime)

Given your array is called $array, you can just do the following:
foreach($array['Direction0'][0]['StopTimes'] as $arr) {
   echo $arr['StopName'];
   echo $arr['Time'];
}


You need to use multiple foreach assuming you can have multiple "DirectionXXX" in the first array and multiple index in each direction array
foreach($myArray as $direction){
    foreach($direction as $subdirection){
        foreach($subdirection['StopTimes'] as $stopTime){
            echo "Stop Name : " . $stopTime['StopName'] . " - Time : " . $stopTime['time'];
        }
    }
}


foreach ($your_array as $direction) {
    foreach ($direction as $id => $direction_data) {         
         foreach ($direction_data['StopTimes'] as $stop_id => $stoptime_data) {
              // finally you got it
              echo $stoptime_data["StopName"];
              echo $stoptime_data["Time"];
         }
    }
}

