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

问题描述 投票:0回答:1

我有一个订单数组。现在,我想根据数组元素的order_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);

但是,当我打印$CompleteOrders数组时,在第一个element(0)中有属于order_id 93的所有订单明细。但是,之后,元素具有不同的键(3和4),即使它们具有相同的order_id,如下所示。

array:3 [▼
  0 => array:3 [▶]
  3 => array:1 [▶]
  4 => array:1 [▼
    0 => {#1268 ▼
      +"id": 102
      +"size": "30"
      +"order_quantity": "1"
      +"price": "798"
      +"created_at": "2020-06-16 19:24:00"
      +"updated_at": "2020-06-16 19:24:00"
      +"order_id": "94"
      +"owi_id": "255"
      +"item_id": "36"
      +"item_name": "Acc One"
      +"description": "Test"
      +"quantity": null
      +"main_fileimg": "1 (1).jpg"
      +"main_filepath": "assets/img/product/accessories/"
      +"main_category": "Accessories"
      +"category": "Denim"
      +"final_total_amount": "1593"
      +"cus_name": "Customer Name"
      +"tel_no": "123456789"
      +"email": "[email protected]"
      +"address_line_one": "Earth Rd"
      +"address_line_two": "Neptune"
      +"city": "Sun"
    }
  ]
]

我想创建一个如下所示的数组。

$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();

我该怎么做?

php arrays laravel associative-array
1个回答
0
投票

似乎您正在过度设计

https://laravel.com/docs/7.x/collections#method-groupby呢?>

我认为您的OrdersWithProductDetails是Laravel集合,您可以使用GroupBy按order_id对它们进行分组

$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);
    
© www.soinside.com 2019 - 2024. All rights reserved.