我有这个数组
Array
(
[availableTrips] => Array
(
[AC] => false
[arrivalTime] => 1830
[availableSeats] => 4
[boardingTimes] => Array
(
[bpId] => 460319
[bpName] => Kalasipalyam
[location] => Kalasipalyam
[prime] => false
[time] => 1215
)
[busType] => Non A/C Semisleeper (2+2)
[busTypeId] => 110
[cancellationPolicy] => 0:8:100:0;8:24:50:0;24:-1:15:0
[departureTime] => 1215
[doj] => 2014-06-18T00:00:00+05:30
[droppingTimes] => Array
(
[bpId] => 559663
[bpName] => Mananthavaadi
[location] => Mananthavaadi
[prime] => false
[time] => 390
)
[fares] => 650
[id] => 100102612640227182
[idProofRequired] => false
[nonAC] => true
[operator] => 5127
[partialCancellationAllowed] => false
[routeId] => 100102600000227182
[seater] => true
[sleeper] => false
[travels] => SKS Travels
[mTicketEnabled] => true
)
)
我从公交车票预订服务的API获得的响应。这是我为特定来源和目的地获得的单一公交车的搜索结果。对于其他来源目的地对,结果可能是类似的,如果路线上有多辆公共汽车。
Array
(
[availableTrips] => Array
(
[0] => Array //1st bus in the search result
(
[AC] => true
[arrivalTime] => 1845
[availableSeats] => 28
[boardingTimes] => Array
(
[0] => Array
(
[bpId] => 46768
[bpName] => Kalasipalyam Main Road
[location] => Kalasipalyam Main Road
[prime] => false
[time] => 1260
)
[1] => Array
(
[bpId] => 46774
[bpName] => Christ University
[location] => Christ University
[prime] => false
[time] => 1280
)
[2] => Array
(
[bpId] => 46771
[bpName] => Madiwala
[location] => Madiwala
[prime] => true
[time] => 1305
)
[3] => Array
(
[bpId] => 46776
[bpName] => Electronic City Toll Gate
[location] => Electronic City Toll Gate
[prime] => false
[time] => 1330
)
[4] => Array
(
[bpId] => 46778
[bpName] => Bommasandra Post Office
[location] => Bommasandra Post Office
[prime] => false
[time] => 1340
)
[5] => Array
(
[bpId] => 46779
[bpName] => Attibele
[location] => Attibele
[prime] => false
[time] => 1350
)
[6] => Array
(
[bpId] => 46780
[bpName] => Hosur
[location] => Hosur
[prime] => false
[time] => 1360
)
)
[busType] => Volvo Multiaxle A/C Seater Semi Sleeper (2+2)
[busTypeId] => 101
[cancellationPolicy] => 0:12:100:0;12:-1:10:0
[departureTime] => 1305
[doj] => 2014-06-18T00:00:00+05:30
[droppingTimes] => Array
(
[0] => Array
(
[bpId] => 46886
[bpName] => Edapally Toll Gate
[location] => Edapally Toll Gate
[prime] => false
[time] => 1825
)
[1] => Array
(
[bpId] => 46884
[bpName] => Palarivattam
[location] => Palarivattam
[prime] => false
[time] => 1830
)
[2] => Array
(
[bpId] => 46882
[bpName] => Vytla junction
[location] => Vytla junction
[prime] => true
[time] => 1845
)
[3] => Array
(
[bpId] => 46972
[bpName] => MG Road
[location] => MG Road
[prime] => false
[time] => 1860
)
)
[fares] => 1000.00
[id] => 200855412640007772
[idProofRequired] => false
[nonAC] => false
[operator] => 7478
[partialCancellationAllowed] => true
[routeId] => 200855400000007772
[seater] => true
[sleeper] => false
[travels] => Shama Transport
[mTicketEnabled] => true
)
[1] => Array //2nd bus in the search result
(
[AC] => true
[arrivalTime] => 1710
[availableSeats] => 33
[boardingTimes] => Array
(
[0] => Array
(
[bpId] => 34106
[bpName] => Kalasipalyam Main Road
[location] => Kalasipalyam Main Road
[prime] => false
[time] => 1080
)
[1] => Array
(
[bpId] => 34099
[bpName] => Dairy circle
[location] => Dairy circle
[prime] => false
[time] => 1095
)
[2] => Array
(
[bpId] => 34098
[bpName] => Christ University
[location] => Christ University
[prime] => false
[time] => 1100
)
[3] => Array
(
[bpId] => 34093
[bpName] => Madiwala
[location] => Madiwala
[prime] => true
[time] => 1170
)
[4] => Array
(
[bpId] => 34103
[bpName] => Electronic City Toll Gate
[location] => Electronic City Toll Gate
[prime] => false
[time] => 1185
)
[5] => Array
(
[bpId] => 34109
[bpName] => Narayana Hridayalaya
[location] => Narayana Hridayalaya
[prime] => false
[time] => 1195
)
[6] => Array
(
[bpId] => 34095
[bpName] => Attibele
[location] => Attibele
[prime] => false
[time] => 1200
)
[7] => Array
(
[bpId] => 34114
[bpName] => Bommasandra Post Office
[location] => Bommasandra Post Office
[prime] => false
[time] => 1210
)
[8] => Array
(
[bpId] => 34112
[bpName] => Hosur
[location] => Hosur
[prime] => false
[time] => 1215
)
)
[busType] => Volvo Multiaxle A/C Seater Semi Sleeper (2+2)
[busTypeId] => 101
[cancellationPolicy] => 0:12:100:0;12:-1:10:0
[departureTime] => 1170
[doj] => 2014-06-18T00:00:00+05:30
[droppingTimes] => Array
(
[bpId] => 45746
[bpName] => Vytala Bye Pass
[location] => Vyttila
[prime] => true
[time] => 1710
)
[fares] => 1100.00
[id] => 200855412640007290
[idProofRequired] => false
[nonAC] => false
[operator] => 6708
[partialCancellationAllowed] => true
[routeId] => 200855400000007290
[seater] => true
[sleeper] => false
[travels] => Atlas Travels
[mTicketEnabled] => true
)
这取决于结果的数量。
这是输出上述结果的代码部分。
$availableBuses = json_decode($availableTrips,true);
echo "<pre>";
print_r($availableBuses);
echo "</pre>";
现在我要做的是,循环并解析通过这个数组输出结果到某些div之类的。
<h1>Search results </h1>
<div>Bus name:<?php echo $ availableBuses['availableTrips']['travels'];?>Boarding place:<?php echo $availableBuses['availableTrips']['boradingTimes']['location']</div>
以上看起来像
**Search Results**
Bus name : SKS Travels
Boarding place : Kalasipalyam
如果有更多结果,则上述内容将重复为
**Search Results**
Bus name : SKS Travels
Boarding place : Kalasipalyam
Bus name : 1 Travels
Boarding place : place 1
Bus name : 2 Travels
Boarding place : place 2
.......
我无法正确解析这个复杂的数组
我试过了
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($availableTrips, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach( $jsonIterator as $output)
{
echo $output['travels'];
echo $output['droppingTimes']['location'];
}
但我无法得到我想要的东西。我想用适当的内容重复div作为公交车增加的数量。
**我知道这可能是一些愚蠢的问题,但我发现它很难通过,谷歌也没有帮助。如果有人在这里可以帮助我将会有很大的帮助
感谢。
请原谅长期问题,没有找到另一种解释方法。
答案 0 :(得分:1)
给出下一个数组:
$availableBuses = Array
(
"availableTrips" => Array
(
"0" => Array //1st bus in the search result
(
"AC" => "true",
"arrivalTime" => "1845",
"availableSeats" => "28",
"boardingTimes" => Array
(
"0" => Array
(
"bpId" => "46768",
"bpName" => "Kalasipalyam Main Road",
"location" => "Kalasipalyam Main Road",
"prime" => "false",
"time" => "1260"
),
"1" => Array
(
"bpId" => "46774",
"bpName" => "Christ University",
"location" => "Christ University",
"prime" => "false",
"time" => "1280"
),
"2" => Array
(
"bpId" => "46771",
"bpName" => "Madiwala",
"location" => "Madiwala",
"prime" => "true",
"time" => "1305"
),
"3" => Array
(
"bpId" => "46776",
"bpName" => "Electronic City Toll Gate",
"location" => "Electronic City Toll Gate",
"prime" => "false",
"time" => "1330"
),
"4" => Array
(
"bpId" => "46778",
"bpName" => "Bommasandra Post Office",
"location" => "Bommasandra Post Office",
"prime" => "false",
"time" => "1340"
),
"5" => Array
(
"bpId" => "46779",
"bpName" => "Attibele",
"location" => "Attibele",
"prime" => "false",
"time" => "1350"
),
"6" => Array
(
"bpId" => "46780",
"bpName" => "Hosur",
"location" => "Hosur",
"prime" => "false",
"time" => "1360"
),
),
"busType" => "Volvo Multiaxle A/C Seater Semi Sleeper (2+2)",
"busTypeId" => "101",
"cancellationPolicy" => "0:12:100:0;12:-1:10:0",
"departureTime" => "1305",
"doj" => "2014-06-18T00:00:00+05:30",
"droppingTimes" => Array
(
"0" => Array
(
"bpId" => "46886",
"bpName" => "Edapally Toll Gate",
"location" => "Edapally Toll Gate",
"prime" => "false",
"time" => "1825",
),
"1" => Array
(
"bpId" => "46884",
"bpName" => "Palarivattam",
"location" => "Palarivattam",
"prime" => "false",
"time" => "1830"
),
"2" => Array
(
"bpId" => "46882",
"bpName" => "Vytla junction",
"location" => "Vytla junction",
"prime" => "true",
"time" => "1845"
),
"3" => Array
(
"bpId" => "46972",
"bpName" => "MG Road",
"location" => "MG Road",
"prime" => "false",
"time" => "1860"
)
),
"fares" => "1000.00",
"id" => "200855412640007772",
"idProofRequired" => "false",
"nonAC" => "false",
"operator" => "7478",
"partialCancellationAllowed" => "true",
"routeId" => "200855400000007772",
"seater" => "true",
"sleeper" => "false",
"travels" => "Shama Transport",
"mTicketEnabled" => "true"
),
"1" => Array //2nd bus in the search result
(
"AC" => "true",
"arrivalTime" => "1710",
"availableSeats" => "33",
"boardingTimes" => Array
(
"0" => Array
(
"bpId" => "34106",
"bpName" => "Kalasipalyam Main Road",
"location" => "Kalasipalyam Main Road",
"prime" => "false",
"time" => "1080",
),
"1" => Array
(
"bpId" => "34099",
"bpName" => "Dairy circle",
"location" => "Dairy circle",
"prime" => "false",
"time" => "1095"
),
"2" => Array
(
"bpId" => "34098",
"bpName" => "Christ University",
"location" => "Christ University",
"prime" => "false",
"time" => "1100"
),
"3" => Array
(
"bpId" => "34093",
"bpName" => "Madiwala",
"location" => "Madiwala",
"prime" => "true",
"time" => "1170"
),
"4" => Array
(
"bpId" => "34103",
"bpName" => "Electronic City Toll Gate",
"location" => "Electronic City Toll Gate",
"prime" => "false",
"time" => "1185"
),
"5" => Array
(
"bpId" => "34109",
"bpName" => "Narayana Hridayalaya",
"location" => "Narayana Hridayalaya",
"prime" => "false",
"time" => "1195"
),
"6" => Array
(
"bpId" => "34095",
"bpName" => "Attibele",
"location" => "Attibele",
"prime" => "false",
"time" => "1200"
),
"7" => Array
(
"bpId" => "34114",
"bpName" => "Bommasandra Post Office",
"location" => "Bommasandra Post Office",
"prime" => "false",
"time" => "1210"
),
"8" => Array
(
"bpId" => "34112",
"bpName" => "Hosur",
"location" => "Hosur",
"prime" => "false",
"time" => "1215"
)
),
"busType" => "Volvo Multiaxle A/C Seater Semi Sleeper (2+2)",
"busTypeId" => "101",
"cancellationPolicy" => "0:12:100:0;12:-1:10:0",
"departureTime" => "1170",
"doj" => "2014-06-18T00:00:00+05:30",
"droppingTimes" => Array
(
"bpId" => "45746",
"bpName" => "Vytala Bye Pass",
"location" => "Vyttila",
"prime" => "true",
"time" => "1710"
),
"fares" => "1100.00",
"id" => "200855412640007290",
"idProofRequired" => "false",
"nonAC" => "false",
"operator" => "6708",
"partialCancellationAllowed" => "true",
"routeId" => "200855400000007290",
"seater" => "true",
"sleeper" => "false",
"travels" => "Atlas Travels",
"mTicketEnabled" => "true"
)
)
);
你可以通过这样做得到你想要的东西:
$workingArrayForBuses = array();
if(isset($availableBuses['availableTrips']['AC'])){
//ONE RESULT
$workingArrayForBuses[] = $availableBuses['availableTrips'];
}else{
//MULTIPLE RESULTS
$workingArrayForBuses = $availableBuses['availableTrips'];
}
echo '<h1>Search results</h1>';
foreach($workingArrayForBuses as $availableBuses){
echo '<div>';
echo 'Bus name : '.$availableBuses['travels'].'<br/>';
$workingArrayForBoardings = array();
if(isset($availableBuses['boardingTimes']['bpId'])){
//ONE RESULT
$workingArrayForBoardings[] = $availableBuses['boardingTimes'];
}else{
//MULTIPLE RESULTS
$workingArrayForBoardings = $availableBuses['boardingTimes'];
}
echo 'Boarding place(s) :<br/>';
foreach($workingArrayForBoardings as $boardingTimes){
echo '- '.$boardingTimes['location'].'<br/>';
}
echo '</div>';
}
那会输出:
Bus name : Shama Transport
Boarding place(s) :
- Kalasipalyam Main Road
- Christ University
- Madiwala
- Electronic City Toll Gate
- Bommasandra Post Office
- Attibele
- Hosur
Bus name : Atlas Travels
Boarding place(s) :
- Kalasipalyam Main Road
- Dairy circle
- Christ University
- Madiwala
- Electronic City Toll Gate
- Narayana Hridayalaya
- Attibele
- Bommasandra Post Office
- Hosur
好吧,我想你需要更新它才能得到你想要的东西。
注意:有一个问题,就是我处理单个结果和多个结果返回数组的方式,问题是返回数组只有一个结果:
Array (
'AC' => true
...
)
而不是
Array (
[0] => array(
'AC' => true
...
)
)
不满足于我曾经处理过的这个技巧,但我不知道如何做到这一点(更好)。如果有人有任何想法,那就太棒了!
希望它有所帮助。