显示一个搜索结果页面,其中包含php中多维数组的值


Showing a search results page which has values from a multidimentional array in php

我有这个数组

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。

**我知道这可能是一些愚蠢的问题,但我发现它真的很难通过谷歌没有帮助。如果这里有人能帮助我,那将是很大的帮助。

谢谢。

请原谅我的问题太长了,我找不到其他的方式来解释这个问题

给定下一个数组:

$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
        ...
    )
)

不满意我用来处理这个问题的技巧,但我不知道如何做另一种(更好的)方法。如果有人有任何想法,我会很高兴知道!

希望能有所帮助。