0

I have a problem with my recursional function. May be you can help me. My function below:

function showTree($items, $level = 0) {

            $arr = [];

            foreach ($items as $item) {
                $arr[] = str_repeat(":", $level * 2) . $item['name'] . "<br />";
                if (!empty($item['children'][0])) {
                    $level++;
                    $arr[] = $this->showTree($item['children'], $level);
                }
            }

            return $arr;
}

And this generate the output:

Array
(
    [0] => Category1

    [1] => Array
        (
            [0] => ::SubCategory2

            [1] => ::SubCategory1

            [2] => Array
                (
                    [0] => ::::SubSubCategory

                )

        )

)

But I need a little bit other data as my output:

 Array
    (
        [0] => Category1
        [1] => ::SubCategory2
        [2] => ::SubCategory1
        [3] => ::::SubSubCategory

    )

Where is my mistake? Thanks!

P>S:

Input:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Category1
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => SubCategory2
                            [parent] => 1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [name] => SubCategory1
                            [parent] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => SubSubCategory
                                            [parent] => 2
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )
)
2
  • You did not decrase the level anywhere. And please, show to the others, what is your input array. Commented Dec 18, 2014 at 13:52
  • Ready. Input data in the question. Commented Dec 18, 2014 at 13:54

2 Answers 2

1

Change this line:

$arr[] = $this->showTree($item['children'], $level);

to:

$arr = array_merge($arr, $this->showTree($item['children'], $level));

I.e. don't add the array returned while walking the children as a new value into the current array but append the values from it to the current array.

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

1 Comment

You can do it this way as well, but note that using array_merge like this can have significant performance implications (ie will be very slow) if your input gets very large.
0

Try this:

function showTree($items, $level = 0, &$arr = array()) {

        foreach ($items as $item) {
            $arr[] = str_repeat(":", $level * 2) . $item['name'] . "<br />";
            if (!empty($item['children'][0])) {
                $level++;
                $this->showTree($item['children'], $level, $arr);
            }
        }

        return $arr;
}

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.