Home » Programming » PHP » Php Array_map

PHP: Process Each Item In An Array with array_map() [Examples]

This article will show you how to process a PHP array using array_map() to run a function on each item in the array.

Why use array_map()?

Arrays store a sequence of values in a single variable. If you’re storing those values, you probably want to do something with them at some point.

Say, for example, you had an array of numbers, and you wanted to calculate the square of each number to be stored in a new array. You could create an empty array, loop through the original array calculating the square of each item, and add it to the new array – or you could use array_map() to generate the new array by running a function on each element of the original.

That’s what the array_map() function is for. It simplifies your code when processing the values in arrays. It gets especially useful when dealing with more complex calculations and large arrays.

PHP array_map() Function Syntax

The syntax for the array_map() function is as follows:

array_map($callback, $array, $arrays)

Note that:

  • array_map() returns a new array
    • If a single array is passed, the indexes are unmodified
    • The original array was not modified
  • $callback is the function that will be applied to each item in the given $array
    • This is usually passed as a string that contains the name of the function to call
  • Multiple additional $arrays can be optionally specified
    • The $callback will accept as many parameters as $arrays are defined
    • Each element in each supplied array will be passed to the $callback with the corresponding values at the same index in the other passed arrays
    • If multiple arrays are passed, numerical indexes will be used in the resulting array based on the order the items in the original arrays were processed – the original keys will not be preserved
    • If the arrays are different lengths, values in an array that is shorter than the others will have a NULL value at the positions that don’t exist in the other arrays

Examples

Here’s a simple example of array_map in action – following from the above, we’ll use array_map() to create a new array containing the squared value of an array of numbers:

function square($num) {
    return $num * $num;
}

$numbers = [1, 2, 3, 4, 5];

$squares = array_map('square', $numbers);
print_r($numbers); // Print the original array of numbers - you will see it is unmodified
print_r($squares); // The resulting array from array_map - 1, 4, 9, 16, 25 - each item from the $numbers array has been squared

As detailed above, multiple arrays can be passed to array_map(). The below example will take the number from an array and multiply it with the corresponding number from a second array:

function multiply($num1, $num2) {
    return $num1 * $num2;
}

$numbers = [1, 2, 3, 4, 5];
$multipliers = [6, 7, 8, 9, 0];

$multiplied = array_map('multiply', $numbers, $multipliers);
print_r($multiplied);// Results in [6, 14, 24, 36, 0]

As you can see, each item in the original arrays is processed in order of appearance by array_map(). Thus, the resulting array of multiplied numbers contains the multiplied values from the original arrays.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment