Essential PHP Array Functions You Should Know

A practical guide to array_map, array_filter, array_reduce, in_array, and array_merge — the PHP array functions used in almost every codebase.

Published September 18, 2026

PHP's array functions cover most transformations you'll need without writing manual loops: mapping, filtering, reducing, searching, and merging.

$doubled = array_map(fn($n) => $n * 2, [1, 2, 3]);
$evens   = array_filter([1, 2, 3, 4], fn($n) => $n % 2 === 0);
$total   = array_reduce([1, 2, 3], fn($carry, $n) => $carry + $n, 0);

Steps

  1. array_map(callback, array) applies a function to every element and returns a new array of the same length
  2. array_filter(array, callback) keeps only elements where the callback returns true, and re-indexes are not automatic — use array_values() to reindex
  3. array_reduce(array, callback, initial) folds the array down into a single value by repeatedly combining elements

How it works

None of these functions mutate the original array — they all return a new array (or value), which fits PHP's general preference for explicit reassignment over in-place mutation for these helpers.

Things to watch for

  • array_filter() preserves original keys, so a filtered array often has gaps — call array_values($filtered) to get sequential keys again
  • array_merge() re-indexes numeric keys but preserves string keys, which trips people up when merging arrays with mixed key types

FAQ

Why does my array have missing indexes after array_filter?

array_filter() keeps the original keys of the elements that pass the test, so removed elements leave gaps. Wrap the result in array_values() to reindex from 0.

More PHP articles