簡體   English   中英

如何“從PHP中的嵌套數組結構獲取數據”

[英]How to 'get data from nested array structure in 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的完整數據。 先感謝您

您可以使用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
    }
}

您可以使用以下循環

為循環

句法 :-

for ($i = 1; $i <= 10; $i++) {
 echo $i;
} 

前瞻

句法 :-

$a = [1,2,3]
foreach ($a as $key => $value) {
    echo "\$a[$k] => $v.\n";
}

如果執行代碼print_r($prepared_arr[1]) ,它將僅獲取數組$ prepared_arr的1th,而忽略0-index,因此它將打印1index的完整數據。 我建議您瀏覽https://www.geeksforgeeks.org/how-to-get-the-first-element-of-an-array-in-php/了解如何訪問php數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM