访问php对象数组的单个值

时间:2019-06-28 10:46:13

标签: php arrays

我调用一个api,得到的响应是

Array (
    [meta] => Array ( 
        [code] => 200 
        [type] => Success 
        [message] => Success 
    ) 
    [data] => Array (
        [items] => Array (
            [0] => Array ( 
                [id] => b4a235596fd9550dfb69f181f4db007f 
                [tracking_number] => 2649884668232181 
                [carrier_code] => hermes 
                [order_create_time] => 
                [destination_code] => 
                [status] => delivered 
                [track_update] => 
                [original_country] => 
                [itemTimeLength] => 7 
                [stayTimeLength] => 74 
                [service_code] => 
                [packageStatus] => 
                [substatus] => 
                [last_mile_tracking_supported] => 
                [origin_info] => Array ( 
                    [ItemReceived] => 2019-04-09 09:29 
                    [ItemDispatched] => 
                    [DepartfromAirport] => 
                    [ArrivalfromAbroad] => 
                    [CustomsClearance] => 
                    [DestinationArrived] => 
                    [weblink] => https://www.hermesworld.com/en/ 
                    [phone] => 
                    [carrier_code] => hermes 
                    [trackinfo] => Array ( 
                        [0] => Array ( 
                                [Date] => 2019-04-15 11:51 
                                [StatusDescription] => Posted through letterbox 
                                [Details] => 
                                [checkpoint_status] => delivered 
                            ) 
                            [1] => Array (
                                [Date] => 2019-04-15 09:45 
                                [StatusDescription] => Delivery will be attempted between 10:00 and 14:00 today 
                                [Details] => 
                                [checkpoint_status] => transit 
                            ) 
                            [2] => Array ( 
                                [Date] => 2019-04-15 06:39 
                                [StatusDescription] => On its way to the courier 
                                [Details] => 
                                [checkpoint_status] => transit 
                            ) 
                            [3] => Array ( 
                                [Date] => 2019-04-14 22:33 
                                [StatusDescription] => At the customers local depot 
                                [Details] => 
                                [checkpoint_status] => transit 
                            )
                            .....
                    ) 
                ) 
                [destination_info] => 
                [lastEvent] => Posted through letterbox,2019-04-15 11:51 
                [lastUpdateTime] => 2019-04-15 11:51 
            )
        ) 
    ) 
) 1 

我想访问 checkpoint_status 的值,但是我无法通过以下方式到达那里:

response in variable 
$track = print_r($track);

1. $track['checkpoint_status']
2. $track[0] // just to see if it returns anything, but no result
3. $track[1] // just to see if it returns anything, but no result
4. array_column($track, 'checkpoint_status'); // returns nothing

3 个答案:

答案 0 :(得分:0)

您需要循环播放以获取所有nodeB

checkpoint_status

示例输出:-https://3v4l.org/IM7I1

答案 1 :(得分:0)

您可以使用array_walk_recursive

 $r = [];
 array_walk_recursive($a, function($v, $k) use(&$r){
  ($k == 'checkpoint_status') ? ($r[]=$v) : '';
 });

https://3v4l.org/rCVEB

答案 2 :(得分:0)

    $response = [
    'meta' => [
        'code' => 200,
        'type' => 'Success',
        'message' => 'Success',
    ],
    'data' =>  [
        'items' =>  [
            '0' =>  [ 
                'id' => 'b4a235596fd9550dfb69f181f4db007f', 
                'tracking_number' => '2649884668232181', 
                'carrier_code' => 'hermes', 
                'order_create_time' => null, 
                'destination_code' => null, 
                'status' => 'delivered', 
                'track_update' => null, 
                'original_country' => null, 
                'itemTimeLength' => 7, 
                'stayTimeLength' => 74, 
                'service_code' => null, 
                'packageStatus' => null, 
                'substatus' => null, 
                'last_mile_tracking_supported' => null,
                'origin_info' =>  [ 
                    'ItemReceived' => '2019-04-09 09:29',
                    'ItemDispatched' =>  null,
                    'DepartfromAirport' =>  null,
                    'ArrivalfromAbroad' =>  null,
                    'CustomsClearance' =>  null,
                    'DestinationArrived' =>  null,
                    'weblink' => 'https://www.hermesworld.com/en/',
                    'phone' => null,
                    'carrier_code' => 'hermes',
                    'trackinfo' =>  [ 
                        '0' =>  [ 
                                'Date' => '2019-04-15 11:51', 
                                'StatusDescription' => 'Posted through letterbox', 
                                'Details' =>null, 
                                'checkpoint_status' => 'delivered', 
                        ],
                        '1' =>  [
                            'Date' => '2019-04-15 09:45',
                            'StatusDescription' => 'Delivery will be attempted between 10:00 and 14:00 today',
                            'Details' => null,
                            'checkpoint_status' => 'transit',
                        ], 
                        '2' =>  [ 
                            'Date' => '2019-04-15 06:39',
                            'StatusDescription' => 'On its way to the courier',
                            'Details' =>null, 
                            'checkpoint_status' => 'transit',
                        ], 
                        '3' =>  [ 
                            'Date' => '2019-04-14 22:33',
                            'StatusDescription' => 'At the customers local depot',
                            'Details' => null,
                            'checkpoint_status' => 'transit',
                        ],
                    ]
                ],
                'destination_info' => null,
                'lastEvent' => 'Posted through letterbox,2019-04-15 11:51',
                'lastUpdateTime' => '2019-04-15 11:51',
            ]
        ]
    ]
];

如果您在开发模式下工作,则可以打开php上的显示错误,并可能得到以下结果:

echo $response['checkpoint_status']; // won't work as response array has no checkpoint_status keyed array
echo $response[0]; // won't work as response array has no 0 keyed array
echo $response[1]; // won't work as response array has no 1 keyed array
print_r(array_column($response, 'checkpoint_status')); // won't work as response array has no column checkpoint_status

您可以通过多种方式获得结果,其中一种方法如下:

$trackinfo = $response['data']['items'][0]['origin_info']['trackinfo'];
$checkpoint_status = array_column($trackinfo, 'checkpoint_status');
print_r($checkpoint_status);

结果:

Array ( 
    [0] => delivered 
    [1] => transit 
    [2] => transit 
    [3] => transit
)