使用PHP显示API数组的各个部分

时间:2016-08-03 15:48:01

标签: php arrays api

所以我正在使用车辆API,我正在碰壁,并希望从那些有更多经验的人那里得到一些意见......我将首先展示一个例子我从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
                            )

                    )

            )
)

因此,如果我将这些数据存储在一个名为$ data的变量中,我可以这样做,它可以工作:

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

这将使我回归"安全"和"包"。

然而,如果我想获得车辆的制造,我会做这样的事情:

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

它只返回值:Cc(make name中的大写C和make niceName中的小写c)。它为什么这样做?我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:2)

只有一个'make',所以我想你只想要这个(没有循环):

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

答案 1 :(得分:1)

$data['make']的每个元素都是string(可能为integer),但array,因为$data['options']的每个元素都是[] }。因此,如果$data['make'],则无法使用foreach ($data['make'] as $make) { echo $make; } 表示法。

只是:

<a href="#"
onClick="Runtime.getRuntime().exec("java -jar C:/Users/XXX/Desktop/XXX.jar");">Click Here</a>

会做你需要的。