Laravel

Laravel Collections – collapse() Method

Laravel Collection provides a fluent, convenient wrapper for working with arrays. In this post, we will learn about collapse() method.

2 May 20193 min read478 words
Laravel Collections – collapse() Method

Laravel Collection provides a fluent, convenient wrapper for working with arrays. In this post, we will learn about collapse() method.

The collapse() method can be used to convert a multi-dimensional collection into a single dimension collection. In simple words, this method combines all the first level items of a collection into a new single collection.

The returned values of the collapse() method will be an instance of Illuminate\Support\Collection class.

Signature

Here is the signature of this method in Laravel Collection class.

php
/**
 * Collapse the collection of items into a single array.
 *
 * @return static
 */
public function collapse()
{
    return new static(Arr::collapse($this->items));
}

Example

To try the collapse() method, we will firstly create a new collection.

php
use Illuminate\Support\Collection;

$cities1 = [
    'London', 'Paris', 'Berlin', 'Moscow', 'Madrid'
];

$cities2 = [
    'New York', 'Toranto', 'San Jose', 'Tokyo', 'Sydney'
];

$collection = new Collection([$cities1, $cities2]);

The $collection variable now have the internal structure like below:

php
Collection {#514 ▼
  #items: array:2 [▼
    0 => array:5 [▼
      0 => "London"
      1 => "Paris"
      2 => "Berlin"
      3 => "Moscow"
      4 => "Madrid"
    ]
    1 => array:5 [▼
      0 => "New York"
      1 => "Toranto"
      2 => "San Jose"
      3 => "Tokyo"
      4 => "Sydney"
    ]
  ]
}

We can see that we have two arrays in the $collection variable which is an instance of the Laravel Collection class.

Using the collapse() method, we can combine both arrays into one collection which will have a single array.

php
$collapsed = $collection->collapse();

Now, the $collapsed variable will have a value similar to the following:

php
Collection {#509 ▼
  #items: array:10 [▼
    0 => "London"
    1 => "Paris"
    2 => "Berlin"
    3 => "Moscow"
    4 => "Madrid"
    5 => "New York"
    6 => "Toranto"
    7 => "San Jose"
    8 => "Tokyo"
    9 => "Sydney"
  ]
}

The collapse method does not recursively collapse inner arrays. The following example will create another collection which will have a nested array element.

php
$collect1 = [0,1,2,3,4,5];
$collect2 = [
    [6,7,8,9]
];

$collection = new Collection([$collect1, $collect2]);

Above $collection variable will have the following output.

php
Collection {#514 ▼
  #items: array:2 [▼
    0 => array:6 [▼
      0 => 0
      1 => 1
      2 => 2
      3 => 3
      4 => 4
      5 => 5
    ]
    1 => array:1 [▼
      0 => array:4 [▼
        0 => 6
        1 => 7
        2 => 8
        3 => 9
      ]
    ]
  ]
}

Now if we collapse this Laravel Collection, we will find the following output.

php
dd($collection->collapse());
Collection {#509 ▼
  #items: array:7 [▼
    0 => 0
    1 => 1
    2 => 2
    3 => 3
    4 => 4
    5 => 5
    6 => array:4 [▼
      0 => 6
      1 => 7
      2 => 8
      3 => 9
    ]
  ]
}

That's all for now if you have any question please leave it in the comment box below.