簡體   English   中英

使用foreach和for循環顯示多維數組

[英]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.

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