2014-06-18 29 views
0

一個多維數組我有這樣的陣列顯示從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']; 
       } 

但是我不能得到我想要的。我想重複與適當的內容作爲公交車數量的增加。

**我知道這可能是周圍的一些愚蠢的問題,但是我發現它真的很難打通,也沒有谷歌並helped.If這裏有人可以幫助我將有很大的幫助

感謝。

請原諒這個長長的問題,沒有找到另一種方式來解釋這一點。

回答

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

不滿意我用來處理這個問題的技巧,但我不知道如何做到這一點(更好)的方式。如果有人有任何想法,將很高興知道!

希望它有幫助。

+0

非常感謝你的回覆。我試試這個。這對一開始真的很有幫助。 – Piya