1

I have an object that implements IteratorAggregate and ArrayAccess, which internally contains an array that will be generated when it is accessed.

I want to use array_walk_recursive on this object, but it does not seem to work. It seems that this is either impossible due to the implementation of array_walk_recursive or I just need to add an interface to my object. What am I missing?

3
  • It does work with ArrayObject. So you might have luck with implementing Traversable and Countable. But OTOH most PHP array functions are a bit recusant with array objects. Commented Nov 14, 2010 at 19:58
  • @mario: You can't directly implement Traversable. It is either implemented by Iterator or IteratorAggregate. Commented Nov 14, 2010 at 20:30
  • It should also work with ArrayIterator Commented Nov 14, 2010 at 22:40

1 Answer 1

2

Since array_walk_recursive operates on arrays, your object probably has to implement the ArrayAccess interface.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, forgot to mention that I implemented ArrayAccess as well.
Then it will work, unless you have an error in your implementation. Remenber that for array_walk_ recursive to work your offsetGet implementation has to return an array or an object of your class.
I realized that the problem is that when I have an array mixed with my object (nested) array_walk_recursive does not work, at least with my current implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.