PHP Array Functions
Essential PHP array functions and patterns
2 min read |223 words
March 18, 2026Array Map
Transform every element in an array:
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(fn($n) => $n ** 2, $numbers);
// [1, 4, 9, 16, 25]
Array Filter
Filter elements by a callback:
$numbers = [1, 2, 3, 4, 5, 6];
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
// [2, 4, 6]
Array Reduce
Reduce an array to a single value:
$items = [
['name' => 'Apple', 'price' => 1.50],
['name' => 'Banana', 'price' => 0.75],
['name' => 'Cherry', 'price' => 2.00],
];
$total = array_reduce($items, fn($carry, $item) => $carry + $item['price'], 0);
// 4.25
Spread Operator
function sum(int ...$numbers): int
{
return array_sum($numbers);
}
$nums = [1, 2, 3];
echo sum(...$nums); // 6
Array Destructuring
$coordinates = [40.7128, -74.0060];
[$lat, $lng] = $coordinates;
// Named keys
$person = ['name' => 'John', 'age' => 30];
['name' => $name, 'age' => $age] = $person;
Related Posts
Singleton Pattern
Implementing the singleton pattern in PHP
phppatternsbackend
PHP References (&)
Useful PHP reference snippets and patterns
phpbackend
For Looping in Python
Using for loops to manipulate arrays in Python.
pythonbackendarrays
While Looping in Python
Using while loops to manipulate arrays in Python.
pythonbackendarrays