A Practical Cheat Sheet for PHP Array Functions

PHP’s array functions are extensive enough that most developers only know a fraction of them — a handful of the less obvious ones eliminate entire loops of manual code.

$nums = [5, 3, 8, 1, 9];

array_map(fn($n) => $n * 2, $nums);          // [10, 6, 16, 2, 18]
array_filter($nums, fn($n) => $n > 4);        // [5, 8, 9] (keys preserved)
array_reduce($nums, fn($c, $n) => $c + $n, 0); // 26

$users = [['name' => 'Alice', 'age' => 30], ['name' => 'Bob', 'age' => 25]];
array_column($users, 'name');                 // ['Alice', 'Bob']
usort($users, fn($a, $b) => $a['age'] <=> $b['age']);

in_array(8, $nums);                           // true
array_unique([1, 2, 2, 3]);                   // [1, 2, 3]
array_chunk($nums, 2);                        // [[5,3],[8,1],[9]]

Reaching for array_map/array_filter/array_reduce instead of manual foreach loops usually produces shorter, more declarative code — worth the habit even though a plain loop is never technically wrong.