PHP Reference ยท Array Functions
PHP array functions overview
A guide to choosing PHP array functions: add and remove, search, transform, combine, compare, sort and aggregate, with a decision table and runnable examples.
PHP has more than seventy array functions, and many of them look alike. This page is a map: it groups the functions by the job you want to do and points out the differences that cause bugs. Each name links to its own reference page.
Three facts explain most of the surprises. First, PHP arrays are ordered maps, so a function may keep, renumber or discard keys. Second, most functions return a new array and leave the original alone, but the sorting functions, array_push(), array_pop(), array_shift(), array_unshift(), array_splice() and shuffle() change the array you pass in place. Third, callbacks run in different argument orders: array_map() takes the callback first, but array_filter() takes the array first.
<?php $prices = [3, 8, 5]; $total = array_sum(array_map(fn(int $p) => $p * 2, $prices)); // 32
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Choosing a function
| I want to... | Use |
|---|---|
| Count elements | count() |
| Add or remove at the end | array_push() (or $a[] = $x), array_pop() |
| Add or remove at the start | array_unshift(), array_shift() |
| Remove or insert in the middle | array_splice() |
| Take a part | array_slice(), array_chunk() |
| Get keys or values | array_keys(), array_values(), array_column() |
| Check a key | array_key_exists(), isset($a[$k]) (false for null values) |
| Check or find a value | in_array(), array_search(), array_find() (PHP 8.4) |
| Test every or any element | array_all(), array_any() (PHP 8.4) |
| Transform each element | array_map(), array_walk() (in place) |
| Keep some elements | array_filter() |
| Reduce to one value | array_reduce(), array_sum() |
| Join arrays | array_merge(), the + and ... operators, array_replace(), array_combine() |
| Compare arrays | array_diff(), array_intersect(), and the _key and _assoc variants |
| Remove duplicates | array_unique() |
| Reverse, flip, pad, fill | array_reverse(), array_flip(), array_pad(), array_fill(), range() |
| Count frequencies | array_count_values() |
| Sort | sort(), asort(), ksort(), usort(), array_multisort() |
| Random choice or shuffle | array_rand(), shuffle() |
Usage notes
- In-place versus new array. Sorting and the push/pop/shift/unshift/splice family modify the variable; almost everything else returns a copy. Reading the first line of the manual signature (
array &$array) tells you. - Keys survive differently.
array_filter()keeps keys (callarray_values()to renumber);array_merge()renumbers integer keys but keeps string keys; the+operator keeps the left array's keys. - Strict comparison.
in_array(),array_search()andarray_keys()with a search value use loose comparison unless you passtrue. Always passtrueunless you want type juggling. array_unique()compares as strings by default (SORT_STRINGflag), so1and"1"are duplicates.- Performance.
array_shift()andarray_unshift()reindex the whole array and cost O(n); for queues useSplQueue.in_array()is O(n); if you search often, flip the array once and useisset(). - Native versus loop. A
foreachis as fast asarray_map()and often clearer when the body is longer than one expression.