Search code examples
phplaravelcollectionslaravel-collection

Restructuring collection items in Laravel


I've got the following Laravel collection of between 800 and 4000 items:

[
  {
    "date": "2017-05-26",
    "departure_time": "14:50:00",
    "arrival_time": "09:20:02",
    "departure_place_id": 16
  },
  {
    "date": "2017-05-26",
    "departure_time": "15:20:00",
    "arrival_time": "15:20:00",
    "departure_place_id": 15
  },
  ...
]

I need to merge always merge two items of the collection into one. So that a collection of 10 items, after merge, is a collection of 5 items with the follwing structure.

[
  {
     "date": "2017-05-26",
     "departure_time": "14:50:00", // from first item
     "arrival_time": "15:20:00", // from second item
     "departure_place_id": 16,  // from first item
     "arrival_place_id": 15 // from second item
  },
  ...
]

As you can see I need to merge data from two consecutive items to one.

I already tried two extend use Laravels custom collection feature but cannot really achieve what I want.

Any clue?


Solution

  • You can solve it by using laravel collection chunk

     public function formatCollection($collection)
     {
      $results = [];
         foreach (collect($collection)->chunk(2) as $chunk)
         {
             $results[] = [
                 'date' => $chunk[0]['date'],
                 'departure_time' => $chunk[0]['departure_time'],
                 'arrival_time' => $chunk[1]['arrival_time'],
                 'departure_place_id'=> $chunk[0]['departure_place_id'],
                 'arrival_place_id' => $chunk[1]['departure_place_id']
             ];
         }
    
        return collect($results);
     }
    

    Call this function to format the data