V
V
VadimKnyzev2012-11-22 02:21:58
PHP
VadimKnyzev, 2012-11-22 02:21:58

How to implement recursive merging of trees?

How can you recursively merge trees?

Trees look like:

$tree1 = new \ArrayObject(<br>
    array(<br>
        array('category' => \Shop\Model\Category, 'child' => \ArrayObject([*RECURSIVE*]))<br>
    )<br>
);<br>

\ArrayObject([*RECURSIVE*]) - well, here it is clear that there can be, in turn, another \ArrayObject object with elements of a similar structure.

Those. in this case, you need to check when merging that if the categories match, then you need to go further through the descendants.
There's a lot going on here, I just can't figure out where to start.
Maybe there is some kind of algorithm?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2012-11-22
@AxisPod

php.net/manual/en/function.array-merge-recursive.php

E
egorinsk, 2012-11-22
@egorinsk

Recursive tree operations are most easily done with a recursive function. Your question is described in such a way that you don’t understand anything, so I’ll give an example of a function that goes through the tree and multiplies each element by 2:
ideone.com/2zVf8C
That is, we make a function that processes one array, and if it has subarrays, we simply call the same function for them. The same approach, it seems to me, will perfectly suit your task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question