Exploring iter\map and array_map a deep dive

jochelle mendonca
3 min readMay 24, 2024

As you progress being a developer, you quickly realize that working efficiently with arrays is a cornerstone of writing clean, maintainable, and performant code.

PHP for one provides several ways to manipulate arrays, with array_map being a familiar function for many. However, there's another powerful tool in the PHP arsenal called iter\map from the iter library.

Let's explore these two functions, their differences, pros and cons, and some tips and tricks for using them effectively.

Photo by AltumCode on Unsplash

Understanding array_map

array_map is a built-in PHP function that applies a callback function to each element of one or more arrays and returns an array containing the results. It’s straightforward and works well for many use cases.

Basic Usage of array_map

$numbers = [1, 2, 3, 4, 5];
$squares = array_map(fn($n) => $n * $n, $numbers);

print_r($squares);
// Output: [1, 4, 9, 16, 25]

In this example, array_map takes an anonymous function that squares each number in the $numbers array.

Pros of array_map

  1. Simplicity: Easy to use and understand.
  2. Multiple Arrays: Can process multiple arrays simultaneously.
  3. Performance: Generally efficient for smaller datasets.

Cons of array_map

  1. Memory Usage: Returns a new array, which can be memory-intensive for large datasets.
  2. Function Signature: The callback function must match the number of arrays passed to array_map.

And now Introducing iter\map

iter\map is a function from the iter library, a set of utilities for working with iterators.

It provides a more memory-efficient way to process arrays or any traversable structure because it operates lazily.

Basic Usage of iter\map

First, you need to install the iter library using Composer:

composer require nikic/iter

Now, you can use iter\map:


require 'vendor/autoload.php';

use iter;
$numbers = [1, 2, 3, 4, 5];
$squares = iter\map(fn($n) => $n * $n, $numbers);
foreach ($squares as $square) {
echo $square . ' ';
}
// Output: 1 4 9 16 25

In this example, iter\map returns an iterator instead of an array, which can be more memory-efficient for large datasets.

Pros of iter\map

  1. Memory Efficiency: Operates lazily, which is beneficial for large datasets.
  2. Flexibility: Works with any traversable, not just arrays.
  3. Composability: Can be easily chained with other iter functions.

Cons of iter\map

  1. Complexity: Slightly more complex to use compared to array_map.
  2. Dependency: Requires the iter library to be installed via Composer.

Comparing iter\map and array_map

+--------------+---------------------------+-------------------------------+
| Feature | array_map | iter\map |
+--------------+---------------------------+-------------------------------+
| Memory Usage | Higher for large datasets | Lower, due to lazy evaluation |
| Ease of Use | Simpler | Slightly more complex |
| Dependency | None (built-in) | Requires iter library |
| Flexibility | Arrays only | Any traversable |
| Performance | Faster for small datasets | Better for large datasets |
+--------------+---------------------------+-------------------------------+

Tips and Tricks

  1. Choosing the Right Tool: Use array_map for smaller datasets or when simplicity is crucial. Opt for iter\map when dealing with large datasets to save memory.
  2. Chaining with iter: Combine iter\map with other iter functions like iter\filter for powerful data processing pipelines.
  3. Fallback to array_map: If you find yourself frequently converting iterators back to arrays, consider whether array_map might be simpler for your use case.
  4. Debugging: When using iter\map, remember that it returns an iterator. Use iterator_to_array to convert it to an array for debugging.

Example of Chaining with iter

$numbers = [1, 2, 3, 4, 5];
$result = iter\map(fn($n) => $n * $n, iter\filter(fn($n) => $n % 2 === 0, $numbers));

foreach ($result as $value) {
echo $value . ' ';
}
// Output: 4 16

In this example, we first filter out odd numbers and then square the remaining even numbers.

Both array_map and iter\map are powerful tools in a PHP developer's toolkit.

Understanding their strengths and limitations allows you to choose the right function for your specific needs.

Whether you prioritize simplicity with array_map or memory efficiency with iter\map, mastering these functions will enhance your ability to manipulate and process arrays effectively in PHP.

Happy coding!

If you liked this article and would like to support me, make sure to:

  • 👏Clap for this story
  • 🔔Follow me on Medium
  • ☕️ If you feel my work is worth an appreciation, you can buy me a coffee!

--

--

jochelle mendonca

Passionate PHP developer. Enthusiastic about the power of words, equally adept at reading and writing