XTRUST.ORG
XTRUST.ORG

Reputation: 3392

php | Bug with recursion

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
                                                (
                                                )

                                        )

                                )

                        )

                )

        )
)

Upvotes: 0

Views: 42

Answers (2)

axiac
axiac

Reputation: 72226

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.

Upvotes: 1

AlpineCoder
AlpineCoder

Reputation: 627

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

Upvotes: 0

Related Questions