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 ships with dozens of array functions, but a handful cover most real work: array_map, array_filter, array_reduce, array_column, array_merge, in_array and usort. Knowing them replaces many hand-written loops.

The concept

array_map transforms every item, array_filter keeps items that pass a test, and array_reduce folds the array into a single value. Together they form a pipeline: filter, map, reduce.

array_column pulls one field out of a list of rows, and usort sorts with your own comparison function. Most of these return a new array and leave the original untouched.

A filter → map → reduce pipeline

  1. Start with [1, 2, 3, 4, 5, 6]
  2. array_filter keeps the evens: [2, 4, 6]
  3. array_map doubles them: [4, 8, 12]
  4. array_sum / array_reduce totals: 24

Practical examples

map, filter, reduce

$n = [1,2,3,4,5,6];
$even = array_filter($n, fn($x) => $x % 2 === 0);
$dbl  = array_map(fn($x) => $x * 2, $even);
$sum  = array_reduce($dbl, fn($c, $x) => $c + $x, 0); // 24

array_column on database rows

$users = [['id'=>1,'name'=>'Ada'],['id'=>2,'name'=>'Bob']];
array_column($users, 'name');          // ['Ada','Bob']
array_column($users, 'name', 'id');    // [1=>'Ada', 2=>'Bob']

Sort by a field

usort($users, fn($a, $b) => $a['name'] <=> $b['name']);

Strict membership check

in_array('1', [1, 2, 3], true); // false (strict)

Common mistakes

  • Forgetting array_filter preserves keys — wrap in array_values() for a clean list.
  • Using in_array without strict=true and getting loose-comparison surprises.
  • Expecting usort to return a new array; it sorts in place.
  • Using array_merge on numeric-keyed arrays when you wanted + (union).

Key takeaways

  • filter → map → reduce covers most loops.
  • array_column extracts fields from rows.
  • Pass true to in_array for strict comparison.

FAQ

array_merge vs +?

array_merge renumbers numeric keys and appends; + keeps the left array's values for duplicate keys.

How do I remove duplicates?

array_unique($arr), then array_values() if you want a reindexed list.

More PHP articles