Pergunta

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)

Foi útil?

Solução

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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top