Can't you check if $roles is traversable (as Doctrine collections are), so that you can iterator_to_array? Then, in the method "current" that of the RecursiveIterator implementation, you check this too for any children, so that you can iterator_to_array the whole hierarchy?