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)

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top