简体   繁体   English

如何使用 foreach 循环创建多维数组

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

I have an array that includes images and orders;我有一个包含图像和订单的数组;

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

This only gets the first image and order.这只会获得第一个图像和订单。 I need to get all the images and orders and execute excute each entry like;我需要获取所有图像和订单并执行每个条目,例如;

 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
     ]

thanks for help.感谢帮助。

Just try 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