N
N
NinjaSystems2020-10-26 10:04:11
PHP
NinjaSystems, 2020-10-26 10:04:11

How to limit the depth of an array when creating?

There is a Yii2 widget that generates a menu of unlimited nesting.
Tree of categories:

Array
(
    [1] => Array
        (
            [id] => 1
            [parent_id] => 0
            [name] => Category 1
            [children] => Array
                (
                    [2] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [name] => Category 1.1
                            [children] => Array
                                (
                                    [3] => Array
                                        (
                                            [id] => 3
                                            [parent_id] => 2
                                            [name] => Category 1.1.1
                                        )

                                )

                        )

                )

        )
    [4] => Array
        (
            [id] => 4
            [parent_id] => 0
            [name] => Category 2
        )
)

I go through the tree recursively and all items are displayed in the menu. In certain places, I need to display a menu with only two levels of nesting. That is, without Category 1.1.1. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Veksler, 2020-10-26
@NinjaSystems

It is possible to bypass recursively and stop at a certain depth.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question