簡體   English   中英

如何使用 foreach 循環創建多維數組

[英]How to create a multidimensional array with foreach loop

我有一個包含圖像和訂單的數組;

 foreach ($images as $image){
   $results => [
            'url' => $image["url"]
            'order' => $image["order"]
        ]
    }

這只會獲得第一個圖像和訂單。 我需要獲取所有圖像和訂單並執行每個條目,例如;

 Array
  (
       [attribute] => [
           [url] => "foo"
           [order] => "boo"
       ]

       [attribute] => [
           [url] => "foo"
           [order] => "boo"
       ]
  )

    //... etc
   
    
    'images' => [
         $results; // need to use the $results array in another array like this
     ]

感謝幫助。

試試這個:

$results = [];
 foreach ($images as $image){
   $results['attribute'][] = [
        'url' => $image["url"],
        'order' => $image["order"]
    ];
 }
 print_r($results);

暫無
暫無

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

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