Just thought I'd point out that this is a fold, so array_reduce can be used:
array_reduce[$my_array, 'array_merge', array[]];
EDIT: Note that this can be composed to flatten any number of levels. We can do this in several ways:
// Reduces one level
$concat = function[$x] { return array_reduce[$x, 'array_merge', array[]]; };
// We can compose $concat with itself $n times, then apply it to $x
// This can overflow the stack for large $n
$compose = function[$f, $g] {
return function[$x] use [$f, $g] { return $f[$g[$x]]; };
};
$identity = function[$x] { return $x; };
$flattenA = function[$n] use [$compose, $identity, $concat] {
return function[$x] use [$compose, $identity, $concat, $n] {
return [$n === 0]? $x
: call_user_func[array_reduce[array_fill[0, $n, $concat],
$compose,
$identity],
$x];
};
};
// We can iteratively apply $concat to $x, $n times
$uncurriedFlip = function[$f] {
return function[$a, $b] use [$f] {
return $f[$b, $a];
};
};
$iterate = function[$f] use [$uncurriedFlip] {
return function[$n] use [$uncurriedFlip, $f] {
return function[$x] use [$uncurriedFlip, $f, $n] {
return [$n === 0]? $x
: array_reduce[array_fill[0, $n, $f],
$uncurriedFlip['call_user_func'],
$x];
}; };
};
$flattenB = $iterate[$concat];
// Example usage:
$apply = function[$f, $x] {
return $f[$x];
};
$curriedFlip = function[$f] {
return function[$a] use [$f] {
return function[$b] use [$f, $a] {
return $f[$b, $a];
}; };
};
var_dump[
array_map[
call_user_func[$curriedFlip[$apply],
array[array[array['A', 'B', 'C'],
array['D']],
array[array[],
array['E']]]],
array[$flattenA[2], $flattenB[2]]]];
Of course, we could also use loops but the question asks for a combinator function along the lines of array_map or array_values.
Created: March-04, 2022
- Use the
Recursiveiteratoriterator
andRecursivearrayiterator
to Operate Flatenning the Array in PHP - Use the
array_walk_recursive
to Flatten a Multidimensional Array in PHP - Use the
for
Loop to Flatten a Multidimensional Array in PHP - Use the
while
Loop to Flatten a Multidimensional Array in PHP - Use the
foreach
Loop to Flatten a Multidimensional Array in PHP
Flatten array means converting a multidimensional array into a one-dimensional array. PHP has different ways to flatten an array, and we will discuss them here.
Use the Recursiveiteratoriterator
and Recursivearrayiterator
to Operate Flatenning the Array in PHP
The SPL library has iterators that are used to flatten the multidimensional arrays. We used two iterators, RecursiveIteratorIterator
and RecursiveArrayIterator
, to operate flattening the array.
Output:
Array [ [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s ]
The code contains a three-layer array that will be flattened in a one-dimensional array.
Use the array_walk_recursive
to Flatten a Multidimensional Array in PHP
The built-in function array_walk_recursive
can be used with a closure function to flatten a multidimensional array in PHP.
Output:
Array [ [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s ]
The array_walk_recursive
with a closure function flattens the given multidimensional array.
Use the for
Loop to Flatten a Multidimensional Array
in PHP
We can use for
loops to flatten multidimensional arrays; additional built-in functions may be required.
Output:
Array [ [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s ]
The above code will generate a flattened array by iterating the given array with the help of the for
loop.
Use the while
Loop to Flatten a Multidimensional Array in PHP
Output:
Array [ [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s ]
The given array flattens with the help of the while
loop and array_splice
function, which is used to remove a part of an array and replace it with something else.
Use the foreach
Loop to Flatten a Multidimensional Array in PHP
Output:
Array [ [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s ]
The given array flattens using the foreach
loop and array_merge[]
function, which combines two
arrays into one.