在表中显示json解码响应

时间:2014-12-09 09:48:33

标签: php json

我有一个JSON响应:

Array (
    [serveTime] => 6
    [places] => Array (
        [0] => Array (
            [kind] => city
            [name] => Delhi
            [longName] => Delhi, India
            [pos] => 28.65381,77.22897
            [countryCode] => IN
            [regionCode] => DL
        )
        [1] => Array (
            [kind] => city
            [name] => Dehradun
            [longName] => Dehradun, India
            [pos] => 30.32556,78.04368
            [countryCode] => IN
            [regionCode] => UT
        )
    )
    [airports] => Array (
        [0] => Array (
            [code] => DEL
            [name] => Delhi
            [pos] => 28.55588,77.08694
            [countryCode] => IN
            [timeZone] => Asia/Kolkata
        )
        [1] => Array (
            [code] => DED
            [name] => Dehra Dun
            [pos] => 30.18972,78.18028
            [countryCode] => IN
            [timeZone] => Asia/Kolkata
        )
    )
    [airlines] => Array (
        [0] => Array (
            [code] => 9I
            [name] => Helitrans
            [url] => http://www.helitrans.no
            [iconPath] => /images/airlines/airlinesD1.gif?v=2014125
            [iconSize] => 27,23
            [iconOffset] => 0,1495
        )
        [1] => Array (
            [code] => 9W
            [name] => Jet Airways
            [url] => http://www.jetairways.com
            [iconPath] => /images/airlines/airlinesD1.gif?v=2014125
            [iconSize] => 27,23
            [iconOffset] => 0,1679
        )
        [2] => Array (
            [code] => S2
            [name] => Jet Lite
            [url] => http://www.jetlite.com
            [iconPath] => /images/airlines/airlinesD7.gif?v=2014125
            [iconSize] => 27,23
            [iconOffset] => 0,897
        )
        [3] => Array (
            [code] => SG
            [name] => SpiceJet
            [url] => http://www.spicejet.com
            [iconPath] => /images/airlines/airlinesD7.gif?v=2014125
            [iconSize] => 27,23
            [iconOffset] => -6,1193
        )
    )
    [aircrafts] => Array (
        [0] => Array (
            [code] => CR7
            [manufacturer] => Canadair
            [model] => CRJ 700
        )
        [1] => Array (
            [code] => 73H
            [manufacturer] => Boeing
            [model] => 737-800
        )
        [2] => Array (
            [code] => DH8
            [manufacturer] => De Havilland Canada
            [model] => DHC-8 Dash 8
        )
        [3] => Array (
            [code] => AT7
            [manufacturer] => ATR
            [model] => ATR 72
        )
    )
    [agencies] => Array (
        [0] => Array (
            [code] => INDIANRAILWAYS
            [name] => Indian Railways
            [url] => http://www.indianrail.gov.in
            [iconPath] => /logos/trains/in.png
            [iconSize] => 27,23
            [iconOffset] => 0,0
        )
        [1] => Array (
            [code] => REDBUS
            [name] => RedBus
            [url] => http://www.redbus.in
            [iconPath] => /logos/trains/rb.png
            [iconSize] => 27,23
            [iconOffset] => 0,0
        )
        [2] => Array (
            [code] => DELHIMETRO
            [name] => Delhi Metro
            [url] => http://www.delhimetrorail.com
            [iconPath] => /logos/trains/delhimetro.png
            [iconSize] => 27,23
            [iconOffset] => 0,0
        )
    )
    [routes] => Array (
        [0] => Array (
            [name] => Train
            [distance] => 309.82
            [duration] => 346
            [totalTransferDuration] => 0
            [indicativePrice] => Array (
                [price] => 8
                [currency] => USD
                [isFreeTransfer] => 0
            )
            [stops] => Array (
                [0] => Array (
                    [name] => New Delhi (NDLS)
                    [pos] => 28.64164,77.22135
                    [kind] => station
                    [code] => NDLS
                    [countryCode] => IN
                    [timeZone] => Asia/Kolkata
                )
                [1] => Array (
                    [name] => Dehradun (DDN)
                    [pos] => 30.31421,78.03365
                    [kind] => station
                    [code] => DDN
                    [countryCode] => IN
                    [timeZone] => Asia/Kolkata
                )
            )
            [segments] => Array (
                [0] => Array (
                    [kind] => train
                    [subkind] => train
                    [vehicle] => train
                    [isMajor] => 1
                    [isImperial] => 0
                    [distance] => 309.82
                    [duration] => 346
                    [transferDuration] => 0
                    [sName] => New Delhi (NDLS)
                    [sPos] => 28.64164,77.22135
                    [sCode] => NDLS
                    [tName] => Dehradun (DDN)
                    [tPos] => 30.31421,78.03365
                    [tCode] => DDN
                    [path] => gaymDmhivM_aBzWiVym@dAsfBmf@gaBw]crCqLenOpn@kaFll@ivBfg@km@rm@w_AaCuv@}t@cWa}BpmAerAiQi`i@ife@cyLceFmUoEimMilCskk@w~Fa`@KurV|oE{rLyQoG}BeiGrUg~AhrAk{nAtfW}nAf}Ai^l}@h^m}@|nAg}Ax@qbBzg@g~CncHi|[liFy_V`T}bAtkLalRmhBcoHm~V_fNkrDipGei@ukAi{@g]kb@lFupL_gGm|@ttAs}@leDidCtxEifAzhA}lBpaAcqO{q@{mCoj@_aF~_DuiCxoD{aBnaEskBphAi^fB
                    [indicativePrice] => Array (
                        [price] => 8
                        [currency] => USD
                        [isFreeTransfer] => 0
                        [nativePrice] => 500
                        [nativeCurrency] => INR
                    )
                    [itineraries] => Array (
                        [0] => Array (
                            [legs] => Array (
                                [0] => Array (
                                    [url] => http://www.cleartrip.com/trains/results?from_station=NDLS&to_station=DDN&class=1A&date=09-12-2014&adults=1&children=0&male_seniors=0&female_seniors=0
                                    [host] => cleartrip.com
                                    [hops] => Array (
                                        [0] => Array (
                                            [sName] => New Delhi (NDLS)
                                            [sPos] => 28.64164,77.22135
                                            [tName] => Dehradun (DDN)
                                            [tPos] => 30.31421,78.03365
                                            [frequency] => 21
                                            [duration] => 346
                                            [indicativePrice] => Array (
                                                [price] => 8
                                                [currency] => USD
                                                [isFreeTransfer] => 0
                                                [nativePrice] => 500
                                                [nativeCurrency] => INR
                                            )
                                            [lines] => Array (
                                                [0] => Array (
                                                    [code] => 12055
                                                    [name] => Ddn Janshatabdi
                                                    [vehicle] => train
                                                    [agency] => INDIANRAILWAYS
                                                    [frequency] => 7
                                                    [duration] => 345
                                                    [days] => 127
                                                )
                                                [1] => Array (
                                                    [code] => 12017
                                                    [name] => Dehradun Shatabdi
                                                    [vehicle] => train
                                                    [agency] => INDIANRAILWAYS
                                                    [frequency] => 7
                                                    [duration] => 350
                                                    [days] => 127
                                                )
                                                [2] => Array (
                                                    [code] => 12205
                                                    [name] => Ndls Ddn Ac Exp
                                                    [vehicle] => train
                                                    [agency] => INDIANRAILWAYS
                                                    [frequency] => 7
                                                    [duration] => 345
                                                    [days] => 127
                                                )
                                            )
                                            [agencies] => Array (
                                                [0] => Array (
                                                    [agency] => INDIANRAILWAYS
                                                    [frequency] => 21
                                                    [duration] => 347
                                                    [actions] => Array (
                                                        [0] => Array (
                                                            [text] => Schedules at
                                                            [url] => http://www.cleartrip.com/trains/results?from_station=NDLS&to_station=DDN&class=1A&date=09-12-2014&adults=1&children=0&male_seniors=0&female_seniors=0
                                                            [displayUrl] => cleartrip.com
                                                            [moustacheUrl] => http://www.cleartrip.com/trains/results?from_station={fromCode}&to_station={toCode}&class=1A&date={day}-{month}-{year}&adults=1&children=0&male_seniors=0&female_seniors=0{dontCombineHops}
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                    [stops] => Array (
                        [0] => Array (
                            [name] => New Delhi (NDLS)
                            [pos] => 28.64164,77.22135
                            [kind] => station
                            [code] => NDLS
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [1] => Array (
                            [name] => Ghaziabad Junction (GZB)
                            [pos] => 28.65278,77.42777
                            [kind] => station
                            [code] => GZB
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [2] => Array (
                            [name] => Meerut City Junction (MTC)
                            [pos] => 28.97766,77.67486
                            [kind] => station
                            [code] => MTC
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [3] => Array (
                            [name] => Muzaffarnagar (MOZ)
                            [pos] => 29.47487,77.70802
                            [kind] => station
                            [code] => MOZ
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [4] => Array (
                            [name] => Saharanpur Junction (SRE)
                            [pos] => 29.96088,77.54253
                            [kind] => station
                            [code] => SRE
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [5] => Array (
                            [name] => Roorkee (RK)
                            [pos] => 29.85194,77.87495
                            [kind] => station
                            [code] => RK
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [6] => Array (
                            [name] => Haridwar Junction (HW)
                            [pos] => 29.94834,78.1555
                            [kind] => station
                            [code] => HW
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                        [7] => Array (
                            [name] => Dehradun (DDN)
                            [pos] => 30.31421,78.03365
                            [kind] => station
                            [code] => DDN
                            [countryCode] => IN
                            [timeZone] => Asia/Kolkata
                        )
                    )
                )
            )
        )
        [1] => Array (
            [name] => Bus
            [distance] => 237.03
            [duration] => 443
            [totalTransferDuration] => 0
            [indicativePrice] => Array (
                [price] => 10
                [currency] => USD
                [isFreeTransfer] => 0
            )
            [stops] => Array (
                [0] => Array (
                    [name] => Delhi
                    [pos] => 28.66849,77.22784
                    [kind] => station
                    [code] => 71867
                    [countryCode] => IN
                    [timeZone] => Asia/Kolkata
                )
                [1] => Array (
                    [name] => Dehradun
                    [pos] => 30.28887,77.99855
                    [kind] => station
                    [code] => 777
                    [countryCode] => IN
                    [timeZone] => Asia/Kolkata
                )
                [2] => Array (
                    [name] => Dehradun
                    [pos] => 30.32556,78.04368
                    [kind] => city
                    [countryCode] => IN
                    [regionCode] => UT
                )
            )
            [segments] => Array (
                [0] => Array (
                    [kind] => bus
                    [subkind] => bus
                    [vehicle] => bus
                    [isMajor] => 1
                    [isImperial] => 0
                    [distance] => 229.95
                    [duration] => 435
                    [transferDuration] => 0
                    [sName] => Delhi
                    [sPos] => 28.66849,77.22784
                    [sCode] => 71867
                    [tName] => Dehradun
                    [tPos] => 30.28887,77.99855
                    [tCode] => 777
                    [path] => ai~mD_qjvM`g@_jB}u@wbCw^miDeBsqEeMctIhAe_@fi@w}Cul@kbAwtIstIkaAml@_l@mt@otJuzIadC_yCe|EskDedBwgAs`Im`I}nC_{B{eDckAkwAiv@esAlFmnBqZqb@dKmiQki@cbMyg@ohNclAguCtHicNpqDuaPbc@_sA}DwoEmp@qPcMa_Afg@kw@dq@ai^dx@sa@z`A{~s@znAc`Wqi]yDk|@wRe^g{J_iJgzBqgCqlIq_GiSkiBge@{g@wxAob@ooDgoB
                    [indicativePrice] => Array (
                        [price] => 8
                        [currency] => USD
                        [isFreeTransfer] => 0
                        [nativePrice] => 480
                        [nativeCurrency] => INR
                    )
                    [itineraries] => Array (
                        [0] => Array (
                            [legs] => Array (
                                [0] => Array (
                                    [url] => http://www.redbus.in/Booking/SelectBus.aspx?fromCityId=71867&fromCityName=Delhi&toCityId=777&toCityName=Dehradun&doj=09-MMM-2014&busType=Any
                                    [host] => redbus.in
                                    [hops] => Array (
                                        [0] => Array (
                                            [sName] => Delhi
                                            [sPos] => 28.66849,77.22784
                                            [tName] => Dehradun
                                            [tPos] => 30.28887,77.99855
                                            [frequency] => 35
                                            [duration] => 435
                                            [indicativePrice] => Array
                                            [price] => 8
                                            [currency] => USD
                                            [isFreeTransfer] => 0
                                            [nativePrice] => 480
                                            [nativeCurrency] => INR
                                    )

我希望它显示在表格中,最有可能是通过循环显示。请帮忙,我现在通过$result['places'][0]['longName'];

手动完成

1 个答案:

答案 0 :(得分:1)

//hope this might help    

    echo "<table>";
        echo "<thead>
               <tr>
               <th>serv time</th>
               <th>places</th>
               -----
               -----
               </tr>
              </thead><tbody>";
          for($i =0 ; $i<sizeof($result); $i++){
             echo "<tr><td>".$result[''servTime]."</td>";
            for($j = 0; $j<sizeof($result['places']); $j++){
              echo "<td>".$result['places'][$i]['longName'];."</td>";
            }
            -----
            echo "</tr>";
          }
        echo "</tbody></table>";