如何遍历此数组。我已经尝试了带有奇怪结果的foreach声明。我需要访问StopName和Time的值。
提前致谢
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
)
此代码最初是以json对象的形式,我使用php json_decode函数将这些数据转换为多维数组。希望我的问题足够明确,以提供见解或解决方案。
答案 0 :(得分:2)
鉴于您的数组名为$array
,您可以执行以下操作:
foreach($array['Direction0'][0]['StopTimes'] as $arr) {
echo $arr['StopName'];
echo $arr['Time'];
}
答案 1 :(得分:1)
你需要使用多个foreach,假设你可以在第一个数组中有多个“DirectionXXX”,在每个方向数组中有多个索引
foreach($myArray as $direction){
foreach($direction as $subdirection){
foreach($subdirection['StopTimes'] as $stopTime){
echo "Stop Name : " . $stopTime['StopName'] . " - Time : " . $stopTime['time'];
}
}
}
答案 2 :(得分:0)
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"];
}
}
}