method flatten documentation in bbn\X
Flattens a multi-dimensional array for the given children index name.
Example
$arr = [
[
'name' => 'John Doe',
'age' => '35',
'children' => [
['name' => 'Carol', 'age' => '4'],
['name' => 'Jack', 'age' => '6'],
]
],
[
'name' => 'Paul',
'age' => '33',
'children' => [
['name' => 'Alan', 'age' => '8'],
['name' => 'Allison 'age' => '2'],
]
],
];
X::flatten($arr, 'children');
/* (array)
[
['name' => 'John Doe', 'age' => '35'],
['name' => 'Paul', 'age' => '33'],
['name' => 'Carol', 'age' => '4'],
['name' => 'Jack', 'age' => '6'],
['name' => 'Alan', 'age' => '8'],
['name' => 'Allison', 'age' => '2']
]
function(array $arr, string $children)
{
$toAdd = [];
$res = self::rmap(
function ($a) use (&$toAdd, $children) {
if (isset($a[$children]) && is_array($a[$children])) {
foreach ($a[$children] as &$c) {
$toAdd[] = $c;
}
unset($c);
unset($a[$children]);
}
return $a;
},
$arr,
$children
);
if (count($toAdd)) {
array_push($res, ...$toAdd);
}
return $res;
}
Flattens a multi-dimensional array for the given children index name. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions