Convert a multi-dimensional array into a single-dimensional array
array_flatten(array $array, bool $preserve_keys = false): array
array
The multi-dimensional array.
preserve_keys
Whether or not to preserve keys in the output array.
The function array_flatten returns single-dimensional array or false if no array is given as the parameter $array.
$ar = ["a", ["b", "c"], [["d",["e","f"]]]];
array_flatten($ar); // ["a","b","c","d","e","f"]
$ar = ["x" => "a", "y" => ["z" => "b", "c"]];
array_flatten($ar); // ["a", "b", "c"]
array_flatten($ar,false); // ["a", "b", "c"]
array_flatten($ar,true); // ["x" => "a", "z" => "b", 0 => "c"]
// !! duplicit key "x"
$ar = ["x" => "a", "y" => ["z" => "b", "x" => "c"]];
array_flatten($ar); // ["a", "b", "c"]
array_flatten($ar,true); // ["x" => "c", "z" => "b"]
Just use the Composer:
composer require yarri/array-flatten
ArrayFlatten is free software distributed under the terms of the MIT license