November 18, 2016 —John Koster
The flatten
helper method is similar to the dot
method in that it takes a multi-dimensional array and transforms it into a new array with only one dimension. While the dot
method preserves the keys by separating them with dots
, the flatten
method will create a new array containing the original array's data whilst throwing away the array's keys.
The signature for the flatten
helper method is:
flatten($array)
Consider the following array:
1<?php2 3$arrayToFlatten = [4 'keys' => 'first value',5 'will' => 'second value',6 'be' => 'third value',7 'removed' => 'fourth value'8];
If we flatten the array like so:
1$newArray = Arr::flatten($arrayToFlatten);
the resulting array would be:
1array(4) {2 [0] "first value"3 [1] "second value"4 [2] "third value"5 [3] "fourth value"6}
array_flatten($array)
The array_flatten
function is a shortcut to calling Arr::flatten
. This function is declared in the global namespace.
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.