Array Functions in PHP: A Practical Reference
The array functions you'll actually reach for day to day — array_map, array_filter, array_reduce, array_column, and array_merge — with real examples.
By DevStudio Online Team · Published September 3, 2026
PHP ships with well over 80 array functions. Most day-to-day work only needs a handful of them used well.
Transform every element: array_map
$prices = [10, 20, 30];
$withTax = array_map(fn($p) => $p * 1.18, $prices);
// [11.8, 23.6, 35.4]array_map always returns an array the same length as the input — use it when you're changing values, not removing them.
Keep only what matches: array_filter
$ages = [22, 34, 19, 41];
$adults = array_filter($ages, fn($a) => $a >= 21);
// [0 => 22, 1 => 34, 3 => 41] — note: keys are preservedThat preserved-keys behavior surprises people — pipe the result through array_values() if you need a clean re-indexed array:
$adults = array_values(array_filter($ages, fn($a) => $a >= 21));Collapse to one value: array_reduce
$cart = [10, 20, 30];
$total = array_reduce($cart, fn($carry, $item) => $carry + $item, 0);
// 60The third argument (0 here) is the initial value — always pass it explicitly, since relying on the default null produces confusing errors the moment your callback does arithmetic.
Pluck a column from an array of arrays: array_column
This one function replaces a foreach loop most people write from scratch:
$users = [
['id' => 1, 'name' => 'Ana'],
['id' => 2, 'name' => 'Ravi'],
];
array_column($users, 'name'); // ['Ana', 'Ravi']
array_column($users, 'name', 'id'); // [1 => 'Ana', 2 => 'Ravi']Combining arrays: array_merge vs. the + operator
$a = ['x' => 1, 0 => 'a'];
$b = ['x' => 2, 0 => 'b'];
array_merge($a, $b); // ['x' => 2, 0 => 'a', 1 => 'b'] — numeric keys renumbered, string keys overwritten
$a + $b; // ['x' => 1, 0 => 'a'] — left array wins on any key conflictThis is the single most common source of "why did my array merge wrong" bugs in PHP — array_merge renumbers integer keys but + never overwrites an existing key from the left-hand array. Pick based on whether numeric-key collisions should be renumbered or preserved.