猿问

如何“从 PHP 中的嵌套数组结构中获取数据”

我试图从数组中获取数据,包括DetectedText值和BoundingBox所有四个值。但面临问题。


完整的数据在'$prepared_arr'中,给定数组的结构如下。


print_r ($prepared_arr);// variable having complete data

Array //output of above array used in pipeline

(

[0] => Array

    (

        [DetectedText] => The number of goals

        [BoundingBox] => Array

            (

                [Width] => 0.66954100131989

                [Top] => 0.04796177148819

                [Left] => 0.2710283100605

                [Height] => 0.072451308369637

            )


    )


[1] => Array

    (

        [DetectedText] => in world cup match

        [BoundingBox] => Array

            (

                [Width] => 0.33683118224144

                [Top] => 0.12350185215473

                [Left] => 0.12564577162266

                [Height] => 0.066131837666035

            )


    )

)

如果我使用 print_r ($prepared_arr[1]) 它只返回索引 1 的完整数据。先感谢您


江户川乱折腾
浏览 187回答 3
3回答

宝慕林4294392

您可以使用foreach从数组中获取数据,例如:// with your exampleforeach($prepared_arr as $val){&nbsp; &nbsp; echo "DetectedText: ". $val['DetectedText']."<br/>"; // using br for line break&nbsp; &nbsp; foreach ($val['BoundingBox'] as $key => $valueInner) {&nbsp; &nbsp; &nbsp; &nbsp; echo $key.": ".$valueInner."<br/>"; // using br for line break&nbsp; &nbsp; }}

慕虎7371278

您可以使用以下循环FOR 循环句法 :-for ($i = 1; $i <= 10; $i++) {&nbsp;echo $i;}&nbsp;FOREACH句法 :-$a = [1,2,3]foreach ($a as $key => $value) {&nbsp; &nbsp; echo "\$a[$k] => $v.\n";}
随时随地看视频慕课网APP
我要回答