2

I have an array that has the variable name $array and it is as follows:

$array = [
    "data"=> [
        [
            "company"=>[
                "id"=> 1,
                "name"=> "company1"
            ],
            "reports"=> [
                "active_reports"=> 3,
                "completed_reports"=> 2
            ]
        ],
        [
            "company"=>[
                "id"=> 2,
                "name"=> "company2"
            ],
            "reports"=> [
                "active_reports"=> 6,
                "completed_reports"=> 1
            ]
        ],
        [
            "company"=>[
                "id"=> 2,
                "name"=> "company2"
            ],
            "reports"=> [
                "active_reports"=> 7,
                "completed_reports"=> 5
            ]
        ]
    ]
];

So, I want to group this array by the company id. The id can be found in the nested array with key company

My expected result is below:

{
  "1": [
    {
      "company": {
        "id": 1,
        "name": "company1"
      },
      "reports": {
        "active_reports": 3,
        "completed_reports": 2
      }
    }
  ],
  "2": [
    {
      "company": {
        "id": 2,
        "name": "company2"
      },
      "reports": {
        "active_reports": 6,
        "completed_reports": 1
      }
    },
    {
      "company": {
        "id": 2,
        "name": "company2"
      },
      "reports": {
        "active_reports": 7,
        "completed_reports": 5
      }
    }
  ]
}

So, I tried the following logic:

foreach ($array['data'] as $data) {

   $reportsData = collect($data)->groupBy($data['company']['id']);

   Log::info($reportsData);
}

But this is the result I'm getting after trying the above logic:

[
  {
    "": [
      {
        "id": 1,
        "name": "company1"
      },
      {
        "active_reports": 3,
        "completed_reports": 2
      }
    ]
  },
  {
    "": [
      {
        "id": 2,
        "name": "company2"
      },
      {
        "active_reports": 6,
        "completed_reports": 1
      }
    ]
  },
  {
    "": [
      {
        "id": 2,
        "name": "company2"
      },
      {
        "active_reports": 7,
        "completed_reports": 5
      }
    ]
  }
]

I want to be able to get the expected result as illustrated above.

0

1 Answer 1

2

You are passing a value to groupBy currently not a 'column' to group by. Though you have nested data here so you will need to pass a callback to groupBy where you will return what value the entities should be grouped by:

collect($array['data'])
    ->groupBy(fn ($item) => $item['company']['id']);

Or simply using the 'dot' notation (as mentioned by Donkarnash), since we are starting the collection at the 'data' key:

collect($array['data'])->groupBy('company.id');

Laravel 9.x Docs - Collections - Available Methods - groupBy

Sign up to request clarification or add additional context in comments.

3 Comments

I guess the dot notation will also work here. Since the desired output doesn't require to manipulate the key, callback is not strictly required, dot notation should suffice collect($array['data'])->groupBy('company.id');
Yes it works in this way @Donkarnash
@Donkarnash yea it will, i forgot to retest that, i had tested it on the original array but not one starting at the 'data' key, works fine ... good call

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.