I have a multidimensional array that looks like this:
$trees = array(
array(
\'name\' => \'Parent\',
\'__children\' => array(
I ended up using something like this, with a lot of inspiration from @Pankrates answer. Thanks a lot.
$trees = $multidimensionalArray;
$flat = array();
$postRepository->flattenRecursive($flat, $trees);
//$flat is now a flattened version of $multidimensionalArray
var_dump($flat);
public function flattenRecursive(array &$flat, array $nested, $parentPrepend = false)
{
foreach( $nested as $item ){
$flat[] = array(
'name' => ($parentPrepend) ? $parentPrepend . '/' . $item['name'] : $item['name']
);
$prepend = $parentPrepend ? $parentPrepend . '/' . $item['name'] : $item['name'];
if(isset($item['__children']))
$this->flattenRecursive($flat, $item['__children'], $prepend);
}
}