Skip to content
Advertisement

How to grouping data array by same value in other array in laravel php

I have some data array like this and i want to regroup this data how to do that and what i suppose to do:

[
 {
    "id":18,
    "order_id":9,
    "invoice":"4345787961",
    "product_id":5,
    "seller_id":1,
    "price":5400000,
    "qty":1,
    "weight":10000,
    "cost":25000,
    "shipping":"JNE-25000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":5425000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
 },
 {
    "id":19,
    "order_id":9,
    "invoice":"2701585840",
    "product_id":4,
    "seller_id":1,
    "price":6199000,
    "qty":1,
    "weight":300,
    "cost":9000,
    "shipping":"JNE-9000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":6208000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
 },
 {
    "id":20,
    "order_id":9,
    "invoice":"4442318215",
    "product_id":1,
    "seller_id":2,
    "price":9249000,
    "qty":2,
    "weight":2000,
    "cost":96000,
    "shipping":"JNT-96000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":18594000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
 }
]

i want grouping data by seller_id if got same seller_id,if seller_id have same data, data push in save array, something like this:

[ 
 [
   {
    "id":18,
    "order_id":9,
    "invoice":"4345787961",
    "product_id":5,
    "seller_id":1,
    "price":5400000,
    "qty":1,
    "weight":10000,
    "cost":25000,
    "shipping":"JNE-25000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":5425000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
  },
  {
    "id":19,
    "order_id":9,
    "invoice":"2701585840",
    "product_id":4,
    "seller_id":1,
    "price":6199000,
    "qty":1,
    "weight":300,
    "cost":9000,
    "shipping":"JNE-9000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":6208000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
  }
],
[
  {
    "id":20,
    "order_id":9,
    "invoice":"4442318215",
    "product_id":1,
    "seller_id":2,
    "price":9249000,
    "qty":2,
    "weight":2000,
    "cost":96000,
    "shipping":"JNT-96000",
    "tracking_number":null,
    "ref":null,
    "ref_status":0,
    "status":"1",
    "total_per_order":18594000,
    "created_at":"2020-07-06T07:44:23.000000Z",
    "updated_at":"2020-07-06T07:45:31.000000Z",
    "status_order":"<span class="badge badge-primary">Dikonfirmasi </span>"
  }
 ]
]

nedd to foreach but i dont know, what i suppose to do..? Any Ideas everyone can help me

Advertisement

Answer

Try this:

collect($array)->groupBy('seller_id');
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement