2016-08-03 6 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']; 
} 

「安全」を私に返し、 "パッケージ"。 CC(メーク名からアッパーケースC、およびから小文字のC:

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

それはちょうど私に値を返します:私は車のメイクを取得したいと私はこのような何かを行う場合は、

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、name、niceNameを組み合わせています。私はそれをすべて必要としません。私には、あなたがちょうど言ったことは、まさに私が書くことを期待して、それを機能させることです。そして、それはしませんでした。 – lpangm03

関連する問題