Answer the question
In order to leave comments, you need to log in
How does the category tree generation function work?
Good day!
On the Internet, I found a function for implementing a category tree. But when it was parsed, I did not finally understand how exactly it works.
The implementation of the function itself is here .
I'm interested in exactly how in this piece of code
foreach ($categories as &$category) {
$map[$category['parent']]['subcategories'][] = &$category;
}
Answer the question
In order to leave comments, you need to log in
in the code from the link you provided, the most subtle point is:
is actually the same (short syntax) as
array_push(
$map[$category['parent']]['subcategories'],
&$category
);
TO greabock :
It's not entirely clear how this recursion is called.
Here I have made the output of the $map array on each iteration of the loop. Could you explain to me how a nested array arises, for example, in the so-called. section with code name Parent => 2--------------------Category => 6 (line 787 ):
[0] => Array
(
[id] => 6
[parent] => 2
[name] => Subcategory F
[subcategories] => Array
(
)
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question