php多维数组,但定位较早的数组以打印出所有姓氏

问题描述 投票:0回答:1

是否有更简单的方法?

    $data = file_get_contents("dumps/datadump-hung1.json");
    $post = json_decode($data, true);

    $currentdriver1st = $post['sessionResult']['leaderBoardLines'][0]['currentDriver']['lastName'];
    $currentdriver2nd = $post['sessionResult']['leaderBoardLines'][1]['currentDriver']['lastName'];   
    $currentdriver3rd = $post['sessionResult']['leaderBoardLines'][2]['currentDriver']['lastName'];      
    echo $currentdriver1st ."<br>" . $currentdriver2nd ."<br>" . $currentdriver3rd ."<br>";

这回显了它应该做什么。

martins
mertesacker
ricciado

不是使用24行代码几乎相同,而是尝试了foreach语句,但是我无法解决我如何提取所有24个驱动程序名称的问题(这里只有3个,这样您就可以了。) >

有没有更简单的方法? $ data = file_get_contents(“ dumps / datadump-hung1.json”); $ post = json_decode($ data,true); $ currentdriver1st = $ post ['sessionResult'] ['leaderBoardLines'] ...

php arrays json multidimensional-array
1个回答
0
投票

您在foreach循环的正确轨道上:

© www.soinside.com 2019 - 2024. All rights reserved.