如何在 php 中使用带有 foreach 的数组制作自定义关联数组
How to make custom associative array using an array with foreach in php
我有一个订单数组。现在我想根据 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
数组时,第一个元素 (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@gmail.com"
+"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();
我该怎么做?
看来你设计过度了
那https://laravel.com/docs/7.x/collections#method-groupby
呢
我认为您的 OrdersWithProductDetails
是一个 Laravel 集合,您可以使用 GroupBy 按 order_id
对它们进行分组
$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);
我有一个订单数组。现在我想根据 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
数组时,第一个元素 (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@gmail.com"
+"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();
我该怎么做?
看来你设计过度了
那https://laravel.com/docs/7.x/collections#method-groupby
呢我认为您的 OrdersWithProductDetails
是一个 Laravel 集合,您可以使用 GroupBy 按 order_id
$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);