简体   繁体   English

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

[英]Get specific value from json response not working

enter image description here在此处输入图像描述

I have the following json response as in the image.如图所示,我有以下 json 响应。 I would want to access specific value, like: "ROUMANIE ROVA AROMANIA" but I can't seem to get to it.我想访问特定的值,例如:“ROUMANIE ROVA AROMANIA”,但我似乎无法获得它。 I tried the following:我尝试了以下方法:

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

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

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

             {

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

}

If I only echo one depth, it's working.如果我只呼应一个深度,它就起作用了。 I searched for a solution but none did the trick.我搜索了一个解决方案,但没有一个成功的。 Thanks.谢谢。

0 is the current index of the first item, $bla contains already the data you're looking for, so doing this directly should work: 0是第一项的当前索引, $bla已经包含您要查找的数据,因此直接执行此操作应该可以:

echo $bla['LineText'];

Here is how the full code should look like:以下是完整代码的样子:


$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'];
     }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM