Skip to content
Tutorials Center
My learning

Topics

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
<?php
$prices = [3, 8, 5];
$total = array_sum(array_map(fn(int $p) => $p * 2, $prices));  // 32

Try it

PHP Keys: kept, renumbered or lost

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.

PHP A small pipeline using several families

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 (call array_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() and array_keys() with a search value use loose comparison unless you pass true. Always pass true unless you want type juggling.
  • array_unique() compares as strings by default (SORT_STRING flag), so 1 and "1" are duplicates.
  • Performance. array_shift() and array_unshift() reindex the whole array and cost O(n); for queues use SplQueue. in_array() is O(n); if you search often, flip the array once and use isset().
  • Native versus loop. A foreach is as fast as array_map() and often clearer when the body is longer than one expression.