0

I'm having trouble to make a tree.

<?php
$x = [];
for($i=0;$i<5;$i++){
    //What I need to do?
}

print_r($x);

I would like to get an Output like this:

Array
(
    [0] => Array
        (
            [1] => Array
                (
                    [2] => Array
                        (
                            [3] => Array
                                (
                                    [4] => 
                                )
                        )
                )
        )
)

Please, guide me how to do that?

1
  • Why loop and not recursion? Commented Jul 18, 2019 at 9:27

1 Answer 1

5

You can alter the way of the for loop to be decreasing and then do:

$x = [];
for($i=4;$i>=0;$i--){
    $x = [$i => $x];
}

Live example: 3v4l

Sign up to request clarification or add additional context in comments.

Comments

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.