Question

I have a ArrayObject structure that is quite complex to output, it can/and consists of multiple levels of relationship e.g. Parent -> Child -> Children -> Child etc.

Structures like this are quite complex to work with when using a foreach, for or while loop. I've looked into SPL Iterators and I think this can be used. I'm a bit unfamiliar with the whole concept so any advice on looping over multiple levels would be well received.

Thanks

Was it helpful?

Solution

In my experience you should just use a recursive function call.

By that I mean you simple make a function that does whatever you need, but have a while loop at the end of the function that loops through the children and calls the function again on each child. This way you fire the function on every child of the original object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top