dongqianzong4275 2019-06-28 10:46
浏览 62
已采纳

访问php对象数组的单个值

I call an api and the response i get is

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 

I would like to access the value of checkpoint_status but im not getting there using the following ways:

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条回答 默认 最新

  • douba2705 2019-06-28 10:55
    关注

    You need to loop, to get all checkpoint_status

    foreach($array['data']['items'] as $item){
       foreach($item['origin_info']['trackinfo'] as $trackinfo){
         echo $trackinfo['checkpoint_status'].PHP_EOL;
       }
    }
    

    Sample output:-https://3v4l.org/IM7I1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题