2016-08-03 23 views
0

因此,我正在使用一個API用於車輛,並且我正在運行到一堵牆中,並希望從中獲得一些有更多經驗的輸入......我將從展示了我從API調用返回的數據的一個示例。用PHP顯示一個API數組的部分

Array 
(
    [make] => Array 
    (
     [id] => 200003644 
     [name] => Chrysler 
     [niceName] => chrysler 
    ) 

[model] => Array 
    (
     [id] => Chrysler_200 
     [name] => 200 
     [niceName] => 200 
    ) 

[engine] => Array 
    (
     [equipmentType] => ENGINE 
     [availability] => USED 
     [cylinder] => 6 
     [size] => 3.6 
     [configuration] => V 
     [fuelType] => flex-fuel (unleaded/E85) 
     [horsepower] => 295 
     [type] => flex-fuel (FFV) 
     [code] => ERB 
     [rpm] => Array 
      (
       [horsepower] => 6350 
       [torque] => 4250 
      ) 

     [valve] => Array 
      (
       [gear] => double overhead camshaft 
      ) 

    ) 

[transmission] => Array 
    (
     [equipmentType] => TRANSMISSION 
     [availability] => USED 
     [transmissionType] => AUTOMATIC 
    ) 

[drivenWheels] => front wheel drive 
[numOfDoors] => 4 
[options] => Array 
    (
     [0] => Array 
      (
       [category] => Safety 
       [options] => Array 
        (
         [0] => Array 
          (
           [id] => 200741607 
           [name] => SafetyTec 
           [description] => Adaptive Cruise Control with Stop & Go; Advanced Brake Assist; Automatic high beam control; Blind Spot and Cross Path Detection; Full Speed Forward Collision Warning Plus; Lane Departure Warning Plus; Parallel and Perpendicular Park Assist with Stop; Rain sensitive windshield wipers 
           [equipmentType] => OPTION 
           [availability] => All C/All C Platinum 
          ) 

        ) 

      ) 

     [1] => Array 
      (
       [category] => Package 
       [options] => Array 
        (
         [0] => Array 
          (
           [id] => 200741480 
           [name] => Quick Order Package 26N (Fleet) 
           [description] => Vehicle with standard equipment 
           [equipmentType] => OPTION 
           [availability] => All C 
          ) 

         [1] => Array 
          (
           [id] => 200741610 
           [name] => Premium Group 
           [description] => 115V auxiliary power outlet; Exterior mirrors with memory; Heated 2 tone leather steering wheel; Luxury door trim panel; Premium leather trimmed ventilated front seats with leather seat cushion; Radio/driver seat/Climate control with memory; Real wood/bronze chrome interior accents 
           [equipmentType] => OPTION 
           [availability] => All C/All C Platinum 
          ) 

         [2] => Array 
          (
           [id] => 200741715 
           [name] => Premium Lighting Group 
           [description] => HID headlamps with LED daytime running lights; LED fog lamps 
           [equipmentType] => OPTION 
           [availability] => All S/All C 
          ) 

         [3] => Array 
          (
           [id] => 200741805 
           [name] => Navigation And Sound Group I 
           [description] => 506 watt amplifier; SiriusXM traffic with 5-year of included service; Travel Link Service with 5-year of included service; 9 amplified speakers with subwoofer; GPS navigation; HD radio; Uconnect 8.4AN AM/FM/SiriusXM/Hard disc drive/Bluetooth/Navigation 
           [equipmentType] => OPTION 
           [availability] => All C 
          ) 

        ) 

      ) 
) 

所以,如果我有存儲在一個變量這個數據名爲$數據,例如,我可以做這樣的事情,它的工作原理:

foreach ($data['options'] as $options) { 
    echo $options['category']; 
} 

,將返回我的「安全」和「包」。

但是,如果我想獲得車輛的品牌和我做這樣的事情:

foreach ($data['make'] as $make) { 
    echo $make['name']; 
} 

它只是返回我的價值:從化妝名抄送(大寫C和小寫字母c從make niceName)。它爲什麼這樣做?我究竟做錯了什麼?

謝謝!

回答

2

這裏只有一個'make',所以我覺得你只是想這樣(沒有循環):

echo $data['make']['name']; 
+0

: - \你說得對,我想這很難。謝謝! – lpangm03

1

$data['make']每一個元素是string(也許integer),但不作爲array$data['options']每一個元素。因此,如果使用$data['make'],則不能使用[]表示法。

剛:

foreach ($data['make'] as $make) { 
    echo $make; 
} 

會做你的需要。

+0

已經嘗試過這一點。我得到這個結果: 200003644Chryslerchrysler 所以它是結合ID,名稱和niceName - 我不需要所有這一切。對我而言,你剛纔所說的正是我所期望寫的,以使其發揮作用。它沒有。 – lpangm03