2

I have a multidimensional nested array:

$arr = array(
    'name'      => 'Root Node',
    'children'  => array(
        array(
            'name'      => 'Node A',
            'children'  => array(
                array(
                    'name'      => 'Node A.1',
                    'children'  => array(
                        array(
                            'name'      => 'Node A.1.1',
                            'children'  => ''
                        ),
                    ),
                ),

                array(
                    'name'      => 'Node A.2',
                    'children'  => ''
                ),
            ),
        ),

        array(
            'name'      => 'Node B',
            'children'  => array(
                array(
                    'name'      => 'Node B.1',
                    'children'  => '',
                ),

                array(
                    'name'      => 'Node B.2',
                    'children'  => ''
                ),
            )
        ),

        array(
            'name'      => 'Node C',
            'children'  => ''
        )
    ),
);

Currently this array is upto 4 levels but we can't assume its depth level static to 4 as it may have N levels as well. My goal is to convert this array with individual row array per relation like:

$arr = array(
    array( 'Root Node', 'Nodel A', 'Node A.1', 'Node A.1.1' ),
    array( 'Root Node', 'Nodel A', 'Node A.2' ),
    array( 'Root Node', 'Nodel B', 'Node B.1' ),
    array( 'Root Node', 'Nodel B', 'Node B.2' ),
    array( 'Root Node', 'Nodel C' ),
);

I know a recursive function should do the trick but I can't figure it out as its not that easy I thought in start. Any help would be greatly appreciated!

3
  • Have you tried anything? Commented Aug 12, 2016 at 10:49
  • use foreach to get in all elements. inside foreach put the values on the table Commented Aug 12, 2016 at 10:50
  • I have tried foreach and recursive functions to make it working but couldn't figure it out Commented Aug 12, 2016 at 11:18

2 Answers 2

2

Use following code snippet. It should resolve your problem.

function makeTreeArr($arr){ 
    $mainarr = []; 
    $tmparr = []; 
    function iterateChildren(&$mainarr,&$tmparr,$node){ 
        $tmparr[] = $node['name']; 
        if(is_array($node['children'])){ 
            foreach($node['children'] as $child){ 
                iterateChildren($mainarr,$tmparr,$child) ;     
            } 
        }else{ 
            $mainarr[] = $tmparr; 
        } 
        array_pop($tmparr ); 
    } 
   iterateChildren($mainarr,$tmparr,$arr); 
   return $mainarr; 
} 
$newarr = makeTreeArr($arr); 
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thanks a lot for your efforts.. However, I have shorten the function further down as: function iterateChildren( $node, $mainarr, $tmparr ) { $tmparr[] = $node['name']; if( is_array( $node['children'] ) ) { foreach($node['children'] as $child){ $mainarr = iterateChildren( $child, $mainarr, $tmparr ); } } else { $mainarr[] = $tmparr; } array_pop( $tmparr ); return $mainarr; }
0

Follow below protocol to fetch the elements as specified by you.

$name0=$arr['name'];
$in1=$arr['children'];
foreach($in1 as $key=>$value)
{
echo $name0.", ";
echo $name1=$in1[$key]['name'].", ";
$in2=$arr['children'][$key]['children'];
    foreach($in2 as $k2=>$v2)
    {
        echo $name2=$in2[$k2]['name'].", ";
        $in3=$arr['children'][$key]['children'][$k2]['children'];
        foreach($in3 as $k3=>$v3)
        {
           echo  $name3=$in3[$k3]['name'].", ";
        }
    }
    echo "</br>";
}

Output :-

Root Node, Node A, Node A.1, Node A.1.1, Node A.2,

Root Node, Node B, Node B.1, Node B.2,

Root Node, Node C,

3 Comments

Thanks for giving it an attempt. Looking at the code snippet, it seems limited to three levels only where I need it to accept any number of levels.
This is 4 level as specified
Doesn't matter either 3 or 4 levels till its static with the script. We can't repeat foreach till 100 times for 100 depth levels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.