I need help in creating a nested array using a loop.

The array should look like this,

Array
(
    [0] => Array
        (
            [id] => 1
            [text] => root
            [leaf] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [text] => root1
                            [leaf] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [text] => root2
                                            [leaf] => 
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 4
                                                            [text] => lastchild
                                                            [leaf] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

The thing is I dont know how to recursivly put the child into the children field. Please help.

what is the data source and its format?

Member Avatar for rajarajan2017

Just show your Array values in a declaration format, output makes confusion?

Im just trying to create the above example using a for loop.
My code is

for($counter = 0; $counter<50; $counter++) {
    $node['id']   = $counter;
    $node['text'] = "Test123";
    $node['leaf'] = rand(0,1);
    $node['children'] = '';
    array_push($tree,$node);
}

This gives an array but its not nested.
I tried many ways to nest it. I just cant seem to get it done.

Member Avatar for rajarajan2017
<?php
$original_items = array();
for($counter = 0; $counter < 4; $counter++) {
	$tree = array();
    $node['id']   = $counter;
    $node['text'] = "Test123";
    $node['leaf'] = rand(0,1);
    $node['children'] = '';
    array_push($tree, $node);
	array_push($original_items,$tree);
}
print_r($original_items);
?>

This will produces your output

Thanks for the above reply. But the output wasnt nested. I need the further arrays in the children field like I shows above.

This is harder than i thought. Is there anybody who can help.

I kinda done at the end. But I honestly believe there is a easy and efficient method. Well heres my code

<?php

//Initial Tree Array
$tree = array();

//Setting leaf to 0 for first iteration
$leaf = 0;

//Creating Tree array
for($counter = 0; $leaf==0; $counter++) {
    $node['id']   = $counter;
    $node['text'] = "Test123";
    $node['leaf'] = rand(0,1);
    $node['children'] = '';
    $leaf=$node['leaf'];
    array_push($tree,$node);
}

echo count($tree);

//Number of nodes
$loopCounter  = (count($tree));

$counter = 0;
$counter1 = 0;

//Last node popped
$n = array_pop($tree);

if($loopCounter > 1) {
    for($c=$loopCounter;$c>0;$c--) {
        if($counter == 0) {
            $n0 = array_pop($tree);
            $n0['children'][] = $n;
        } else {
            $a = array_pop($tree);
            $a['children'][] = $n0;
            $n0=$a;
        }
        $counter++;
    }
}

echo "<pre>";
$treeStructure = array();
if($loopCounter == 1) {
    array_push($treeStructure,$n);
    print_r($treeStructure);
} else {
    array_push($treeStructure,$a['children']);
    print_r($treeStructure);
}
echo "</pre>";
exit;
?>

]

Member Avatar for rajarajan2017

Little bit more complex. but U got it! Thanks for sharing the code.

Im all open for a better solution. thanks for the replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.