0

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

2
  • 1
    you can use map() or groupBy() it depends on your use. Commented Jul 6, 2020 at 9:00
  • can you show me the code? Commented Jul 6, 2020 at 9:01

2 Answers 2

2

Try this:

collect($array)->groupBy('seller_id');
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, you read the question correctly. Group by, not key by :) +1 and I'll delete my answer
1

First you need tot query the product.

$products = Product::all();

You can use groupBy()

return $products->groupBy('seller_id');

Comments

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.