Converting the tree structure of an object into an array and the interaction of the entire object

I have a tree structure of an object consisting of two types of objects:

  • class object Category
  • class object CategoryLink

The structure is as follows:

The whole story begins with an array Categoriesthat does not have one. parent Each Categoryhas several non-essential properties and several important ones:
$parent- containing the parent id Category,
$children- contain childern in the array Categories(can be empty if the category does not have childern, of course).
$links- containing an array CategoryLinks(also possibly empty)

While __constructinga Category, I am looking for an existing child Categoriesand CategoryLinks, and if there are some, I create copies of them and add them to $childrenand $links, therefore, this procedure is repeated for children and their children, etc., until a category without children is reached.

So what this procedure does is that it basically creates a tree Categoriesand them Links. This happens well enough until I want to derive this tree structure (using Smarty), and I'm not quite sure how to iterate over correctly. The desired conclusion is something like this.

Parent1 -its unimportant properties
    -Child1 - its unimportant properties
    -Child2 -...
        -Child2 Child1
        -Child2 Child2
    -Child3
Parent2
    -Child1
        -Child1 Child1
    -Child2
Parent3
...

I'm not sure if it's better to iterate over it in PHP and convert it to a multidimensional array and iterate over it in Smarty or do it completely in Smarty.

* , CategoryLink, Category , , m , .

| ?

+3
2
. . . , , .
+3

SPL, , - :

RecursiveIterator. :

$iterator = new RecursiveIteratorIterator(
    $theMostTopParent
);
foreach ($iterator as $category) {
     // your code
}

, SPL, , - . , ?

: Smarty foreach-loop, smarty.

+2

Source: https://habr.com/ru/post/1755799/


All Articles