[英]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
}
}
如果執行代碼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.