yarri / array-flatten
Convert a multi-dimensional array into a single-dimensional array
Installs: 1 403
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
Requires (Dev)
- atk14/tester: *
This package is auto-updated.
Last update: 2024-10-21 16:58:47 UTC
README
Convert a multi-dimensional array into a single-dimensional array
Function signature
array_flatten(array $array, bool $preserve_keys = false): array
Parameters
array
The multi-dimensional array.
preserve_keys
Whether or not to preserve keys in the output array.
Return value
The function array_flatten returns single-dimensional array or false if no array is given as the parameter $array.
Usage
$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"]
Installation
Just use the Composer:
composer require yarri/array-flatten
License
ArrayFlatten is free software distributed under the terms of the MIT license