views:

28

answers:

1

Hi,

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

+1  A: 

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.

laurencek
laurencek I've handled the problem this way in the past but SPL seems to have classes already in place to handle it e.g. RecursiveArrayIterator() and RecrusiveIteratorIterator().
I've looked at those briefly but never used them. Recursive function calls have always been sufficient for my requirements.
laurencek