首页 > 解决方案 > 如何“从 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 的完整数据。先感谢您

标签: phparrays

解决方案


您可以使用foreach从数组中获取数据,例如:

// with your example
foreach($prepared_arr as $val)
{
    echo "DetectedText: ". $val['DetectedText']."<br/>"; // using br for line break
    foreach ($val['BoundingBox'] as $key => $valueInner) {
        echo $key.": ".$valueInner."<br/>"; // using br for line break
    }
}

推荐阅读