[英]how to display values of a multidimensional associative array using foreach loop
[英]Display multidimensional array using foreach and for loop
for(int $i=0; $i<count($lists["item_collection"]["entries"]); $i++){
foreach($lists['item_collection']['entries'] as $list)
{
$print['file_name'] = $list[$i]['name'];
$print['file_id'] = $list[$i]['id'];
$print['file_type'] = $list[$i]['type'];
array_push($content,$print);
}
}
我試圖獲取文件數組的名稱,類型,ID,但我只得到第一個,所以我嘗試對其進行循環,但它不起作用。 我這樣做對嗎?
我沒有測試
foreach($lists["item_collection"]["entries"] as $v){
foreach($v as $val){
$print['file_name'] = $val['name'];
$print['file_id'] = $val['id'];
$print['file_type'] = $val['type'];
array_push($content,$print);
}
}
無需在for循環內執行此操作,只需:
foreach($lists["item_collection"]["entries"] as $key => $val){
$print['file_name'] = $val['name'];
$print['file_id'] = $val['id'];
$print['file_type'] = $val['type'];
array_push($content,$print);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.