PHP lazy array mapping

Viewed 3685

Is there a way of doing array_map but as an iterator?

For example:

foreach (new MapIterator($array, $function) as $value)
{
   if ($value == $required)
      break;
}

The reason to do this is that $function is hard to calculate and $array has too many elements, only need to map until I find a specific value. array_map will calculate all values before I can search for the one I want.

I could implement the iterator myself, but I want to know if there is a native way of doing this. I couldn't find anything searching PHP documentation.

8 Answers
Related