0

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.

Edited by blessan: n/a

3
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by blessan
0

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

Edited by rajarajan07: n/a

0

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.

Edited by blessan: n/a

0
<?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

0

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

0

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

0

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;
?>

]

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.