donglan9651 2016-08-03 15:48
浏览 44
已采纳

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

So I'm working with an API for vehicles, and I am running into a wall and hoping to get some input from those with more experience with this... I'll start by showing an example of the data I get back from an API call.

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
                            )

                    )

            )
)

So if I have this data stored in a variable called $data for example, I can do something like this and it works:

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

That will return me "Safety", and "Package".

However, if I wanted to get the vehicle's make and I do something like this:

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

It just returns me the value : Cc (Upper case C from the make name, and lowercase c from the make niceName). Why is it doing this? What am I doing wrong?

Thank you!

  • 写回答

2条回答 默认 最新

  • dpjw67160 2016-08-03 15:51
    关注

    There's only one 'make', so I think you just want this (no loop):

    echo $data['make']['name'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样