Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用带有foreach的数组创建自定义关联数组_Php_Arrays_Laravel_Associative Array - Fatal编程技术网

如何在php中使用带有foreach的数组创建自定义关联数组

如何在php中使用带有foreach的数组创建自定义关联数组,php,arrays,laravel,associative-array,Php,Arrays,Laravel,Associative Array,我有一个订单数组。现在我想根据数组元素的顺序\u id对它们进行分组。然后我确实喜欢下面 $CompleteOrders = []; $OrderCount = 0; foreach($OrdersWithProductDetails as $OrdersWithProductDetail) { if(empty($CompleteOrders)) { $CompleteOrders[$OrderCoun

我有一个订单数组。现在我想根据数组元素的
顺序\u id
对它们进行分组。然后我确实喜欢下面

$CompleteOrders = [];

        $OrderCount = 0;
        foreach($OrdersWithProductDetails as $OrdersWithProductDetail) {

            if(empty($CompleteOrders)) {
                $CompleteOrders[$OrderCount][$OrderCount] = $OrdersWithProductDetail;
            } else {
                for($i = 0; $i < count($CompleteOrders); $i++) {
                    if(isset($CompleteOrders[$i][$i])) {
                        if($CompleteOrders[$i][$i]->order_id == $OrdersWithProductDetail->order_id) {
                            $CompleteOrders[$i][$OrderCount+1] = $OrdersWithProductDetail;
                            //print_r($CompleteOrders);
                            $OrderCount++;
                        } else {
                            $CompleteOrders[$OrderCount+1][0] = $OrdersWithProductDetail;
//                        print_r($CompleteOrders);
                            $OrderCount++;
                        }
                    }
                }
//                break;
            }

        }

        dd($CompleteOrders);
我想创建一个如下所示的数组

$Orders = 
    0 => [
         0 => "OrderId94"
              "Item"
         1 => "OrderId94"
              "Item"
],
    1 => [
         0 => "OrderId94"
              "Item"
         1 => "OrderId94"
              "Item"
]
**编辑**

$OrdersWithProductDetails = DB::table('order_id_with_owi_ids')
            ->join('orders_with_items','orders_with_items.id','order_id_with_owi_ids.owi_id')
            ->join('item_id_with_owi_ids','item_id_with_owi_ids.owi_id','orders_with_items.id')
            ->join('products','products.item_id','item_id_with_owi_ids.item_id')
            ->join('item_with_main_categories','item_with_main_categories.item_id','products.item_id')
            ->select('orders_with_items.*', 'order_id_with_owi_ids.*', 'products.*')
            ->get();

我该怎么做呢?

看起来你在过度设计

那怎么办

我认为您的
OrdersWithProductDetails
是一个Laravel集合,您可以使用GroupBy按订单id对其进行分组

$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);

尝试使用
$data=json\u encode($CompleteOrders)谢谢,它工作得很好!!我还想知道,如果不使用groupBy,我怎么能得到这个呢。我的意思是使用foreach或任何其他方法??使用普通数组可以获得相同的结果,但这不是laravel方法,使用集合可以做更有趣的事情,明白了吗。。但是,在我的例子中,我用foreach和for循环做了什么错事??
$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);