PHP array_map() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_map() applies a callback to every item and returns a new array of results.

It is the standard way to transform every value, such as doubling numbers or uppercasing words, without writing a loop.

Syntax

Call array_map() with the values you need. Start with this line.

PHP
$result = array_map($callback, $array);
CallWhat it does
array_map(fn($n) => $n * 2, [1, 2, 3])Returns [2, 4, 6].
array_map("strtoupper", ["a", "b"])Returns ["A", "B"].
array_map(null, [1, 2], ["a", "b"])Zips arrays together into [[1, "a"], [2, "b"]].

Note the argument order: the callback comes first, then the array. This is the opposite of array_filter().

Library declarations
PHP
array_map(?callable $callback, array $array, array ...$arrays): array

Parameters

ParameterDescription
callbackRequired. A function that receives one item from each array. Use null to zip arrays together.
arrayRequired. The first array to process.
arraysOptional. More arrays. The callback then receives one item from each array.

Return Value

Return ValueDescription
arrayA new array of the values returned by the callback.

PHP Examples

Example 1 — Double Every Number

Apply a small arrow function to each item.

PHP
<?php
$numbers = [1, 2, 3];

$doubled = array_map(fn($n) => $n * 2, $numbers);

echo implode(", ", $doubled), PHP_EOL;

How It Works

  1. Create an array. It holds the numbers 1, 2 and 3.
  2. Write the callback. fn($n) => $n * 2 returns twice its input.
  3. Call array_map(). PHP applies the callback to each item in order.
  4. Print the new array. The original $numbers is unchanged.

Example 2 — Two Arrays and the null Callback

Pass more than one array to combine them, or null to zip them.

PHP
<?php
$sum = array_map(fn($a, $b) => $a + $b, [1, 2, 3], [10, 20, 30]);
echo implode(", ", $sum), PHP_EOL;

$pairs = array_map(null, [1, 2], ["a", "b"]);
echo json_encode($pairs), PHP_EOL;

How It Works

  1. Pass two arrays. The callback receives one item from each at the same position.
  2. Add the pairs. It adds 1+10, 2+20 and 3+30.
  3. Use null as callback. PHP simply groups the items together.
  4. Print as JSON. Each inner array holds one number and one letter.

Example 3 — Keep String Keys

With a single array, array_map() keeps the original keys.

PHP
<?php
$names = ["first" => "ana", "last" => "diaz"];

$upper = array_map("strtoupper", $names);

echo json_encode($upper), PHP_EOL;

How It Works

  1. Create an array with named keys. The keys are first and last.
  2. Pass a function name. You can use the string "strtoupper" as the callback.
  3. Call array_map(). The values are changed and the keys are kept.
  4. Print as JSON. The names are now uppercase.

Frequently Asked Questions

It applies a callback to each item of an array and returns a new array with the results. The original array is not changed.
array_map(?callable $callback, array $array, array ...$arrays): array. The callback is the first argument.
Only when you pass a single array. With several arrays, the result uses numbered keys.
array_map() returns a new array. array_walk() changes the original array in place and does not return a new one.
The callback only receives values. To use keys too, pass array_keys($array) as a second array, or use array_walk().

Next: array_merge()

array_merge() joins one or more arrays into a single new array.

array_merge() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful