Laravel Collections – collapse() Method
Laravel Collections ( 11 Lessons )
Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.
see full series- Laravel Collections – Introduction to Collections
- Laravel Collections – make Method
- Laravel Collections – wrap() and unwrap() Methods
- Laravel Collections – map() Method
- Laravel Collections – times() Method
- Laravel Collections – collapse() Method
- Laravel Collections – contains() & containsStrict() Methods
- Laravel Collections – each() Method
- Laravel Collection – Using toJson() Method
- Laravel Collection – Filtering Collection Items using where()
- Laravel Collection – Using splice() Method to Remove Items
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.
/** * 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.
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:
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.
$collapsed = $collection->collapse();
Now, the $collapsed
variable will have a value similar to the following:
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.
$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.
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.
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.