Array Functions
Prerequisites: Arrays: indexed and associative
PHP's standard library has dozens of array functions —
count(), array_map(), array_filter(), array_reduce(),
in_array(), sort() cover most everyday needs.
$doubled = array_map(fn ($n) => $n * 2, [1, 2, 3]); // [2, 4, 6]
$users = [['name' => 'Ada', 'age' => 36], ['name' => 'Al', 'age' => 12]];
$adults = array_filter($users, fn ($u) => $u['age'] >= 18);
$total = array_reduce([1, 2, 3, 4], fn ($carry, $n) => $carry + $n, 0); // 10
Common mistakes
- Forgetting
array_filterpreserves original keys — re-index witharray_values()afterward if a clean0, 1, 2, ...sequence is needed.