Question

I'm having a few problems with ArrayIterator (And, indeed, the same problem with ArrayObject).

For 99% of everything, my extended ArrayIterator behaves like an array and is working great.

Unfortunately, implode() does not like being given an ArrayIterator (or ArrayObject).

I can't spot in the docs anywhere which suggests any other classes to implement on by extended ArrayIterator, nor any other methods to override.

Can anyone suggest how to get this working? (Note: Casting to an array every time I use implode is not a solution, as I'd like this array-like object to work EXACTLY as an array, and not have the code using it to have to know/care/cast)

Was it helpful?

Solution

The easiest correct solution is to use iterator_to_array to feed implode, e.g.

$traversable = /* your iterator, ArrayObject or any other type of Traversable */
echo implode(",", iterator_to_array($traversable));

This will work as expected with anything that can be iterated with foreach.

OTHER TIPS

try downcasting the array ((array) $arrayObject) : implode(",", (array) $arrayObject);

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