PHP array_map Function
last modified March 13, 2025
The PHP array_map
function applies a callback to each element of
one or more arrays. It returns a new array with transformed elements.
Basic Definition
The array_map
function processes each array element through a
callback function. It creates a new array with the returned values.
Syntax: array_map(callable $callback, array $array, array ...$arrays): array
.
The callback receives array elements as arguments and returns transformed values.
Basic array_map Example
This example demonstrates squaring each number in an array using array_map.
<?php declare(strict_types=1); $numbers = [1, 2, 3, 4, 5]; $squared = array_map(fn($n): int => $n * $n, $numbers); print_r($squared); // Output: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
The callback squares each number. array_map
returns a new array
with the squared values while preserving the original array's structure.
Processing Multiple Arrays
array_map
can process multiple arrays simultaneously.
<?php declare(strict_types=1); $names = ["Alice", "Bob", "Charlie"]; $ages = [25, 30, 35]; $combined = array_map( fn($name, $age): string => "$name is $age years old", $names, $ages ); print_r($combined); // Output: Array ( [0] => Alice is 25 years old [1] => Bob is 30 years old... )
This combines corresponding elements from two arrays into strings. The callback receives one element from each array in the same position.
Using Named Functions
You can use named functions instead of anonymous functions for complex logic.
<?php declare(strict_types=1); function formatPrice(float $price): string { return '$' . number_format($price, 2); } $prices = [19.99, 29.95, 9.50]; $formatted = array_map('formatPrice', $prices); print_r($formatted); // Output: Array ( [0] => $19.99 [1] => $29.95 [2] => $9.50 )
The formatPrice
function formats each price with a dollar sign
and two decimal places. The function name is passed as a string to array_map.
Working with Array Keys
array_map
doesn't preserve keys by default, but we can work around this.
<?php declare(strict_types=1); $data = ['a' => 1, 'b' => 2, 'c' => 3]; $result = array_map( fn($value, $key): array => [$key => $value * 2], $data, array_keys($data) ); $final = array_merge(...$result); print_r($final); // Output: Array ( [a] => 2 [b] => 4 [c] => 6 )
This example shows how to preserve keys by passing array_keys as a second array. The result is then merged to recreate the original associative array structure.
Advanced: Using array_map with Objects
array_map
can transform object properties or call methods.
<?php declare(strict_types=1); class User { public function __construct( public string $name, public string $email ) {} public function getDomain(): string { return explode('@', $this->email)[1]; } } $users = [ new User('Alice', 'alice@example.com'), new User('Bob', 'bob@domain.com'), new User('Charlie', 'charlie@test.com') ]; $domains = array_map(fn(User $u): string => $u->getDomain(), $users); print_r($domains);
This extracts email domains from User objects. The callback calls the getDomain method on each object, demonstrating object-oriented use of array_map.
Best Practices
- Type Safety: Use type hints in callbacks for better code reliability.
- Readability: Prefer named functions for complex transformations.
- Performance: Avoid expensive operations in large array processing.
- Immutability: Remember array_map creates new arrays without modifying originals.
Source
This tutorial covered the PHP array_map
function with practical
examples showing its usage for array transformation scenarios.
Author
List all PHP Array Functions.