0

I have a Collection class that holds data as an array internally.

class Collection implements \IteratorAggregate, \Countable, \ArrayAccess
{
...
}

At some point in the code, following is happening

$list = new Collection([]);

array_map(function(Object $v){ return $v->getKey(); }, $list);

I thought implementing ArrayAccess should be enough to make an object behave like an array. IN thins case I have other implemented aS well, but clearly php uses some other mechanism to detect if the argument passed is an array as I am getting the

array_map(): Argument #2 should be an array
Scalable
  • 1,550
  • 4
  • 16
  • 29
  • 2
    array_map really only takes arrays as far as I know. I don't think you can use it with other things just by making them iterable/array accessible. – Don't Panic Feb 17 '17 at 21:17
  • 1
    I think this answers your question: http://stackoverflow.com/questions/16273233/array-map-on-collection-with-array-interfaces – Theo Feb 17 '17 at 21:21
  • Thanks iterator_to_array($list) worked. – Scalable Feb 17 '17 at 21:26

0 Answers0