从json响应中获取特定值不起作用

时间:2019-11-08 17:13:23

标签: php arrays

enter image description here

我在图像中有以下json响应。我想访问特定的值,例如:“ ROUMANIE ROVA AROMANIA”,但我似乎无法理解。我尝试了以下方法:

$response =  json_decode($r->getBody(),true);

foreach($response['ParsedResults'] as $key)
{

             foreach($key['TextOverlay']['Lines'] as $bla)

             {

                 echo $bla['LineText'];
                 echo $bla[0]['LineText'];
             }

}

如果我仅回声一个深度,则它起作用。我寻找解决方案,但没有成功。谢谢。

1 个答案:

答案 0 :(得分:1)

0是第一项的当前索引,$bla已包含您要查找的数据,因此直接执行此操作应该有效:

echo $bla['LineText'];

完整代码如下所示:


$response = [
    'ParsedResults' => [
        [
            'TextOverlay' => [
                    'Lines' => [
                            [
                                'LineText' => 'ROUMANIE ROVA AROMANIA',
                                'Words' => [
                                        [
                                            'WordText' => 'ROUMANIE',
                                            'OtherData' => 'whatever'
                                        ],
                                        [
                                            'WordText' => 'ROVA',
                                            'OtherData' => 'whatever'
                                        ],
                                        [
                                            'WordText' => 'AROMANIA',
                                            'OtherData' => 'whatever'
                                        ],
                                    ]
                            ]
                        ]
                ]
        ]
    ]
];

foreach($response['ParsedResults'] as $key)
{
     foreach($key['TextOverlay']['Lines'] as $bla)
     {
         echo $bla['LineText'];
     }
}

在这里测试:http://sandbox.onlinephpfunctions.com/code/0577e854eed73dfb33193c391acc37dd81baf982