0

I have an orders array. Now I want to group array elements according to their order_id. Then I did like below...

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

But, when I print the $CompleteOrders array, in first element(0) there are all the order details which is belongs to order_id 93. but, after that elements have different keys(3 and 4) even they has the same order_id like below.

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"
    }
  ]
]

I want to create an array like below.

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

** EDIT **

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

How can I do this?

1
  • Try with $data = json_encode($CompleteOrders); Commented Jun 17, 2020 at 18:34

1 Answer 1

1

It looks like you are overengineering

What about https://laravel.com/docs/7.x/collections#method-groupby

I think your OrdersWithProductDetails is a Laravel Collection you can use GroupBy to group them by the order_id

$grouped = $OrdersWithProductDetails->groupBy('order_id');
$grouped = $grouped->toArray();
dd($grouped);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You, it worked perfectly !! And also I want to know, how can I get this without using groupBy. I mean using foreach or any other method ??
Well you can achieve the same result using plain arrays but that's not the laravel way, with collections you can do more interesting things
Okay, got it.. But, in my example, what am I doing wrong with that foreach and for loop ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.