PHP Array Functions

About This Index
This page is the PHP Array Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn how arrays, callbacks, merging, and sorting fit together on /php/array. Use this page when you need a specific function such as count(), array_map(), or array_merge().
Start with count() and in_array()—then learn array_push() and array_pop(), array_map(), array_filter() and array_reduce(), and finish with array_merge() and sort().
array_is_list() needs PHP 8.1, array_find(), array_find_key(), array_any() and array_all() need PHP 8.4, and array_first() and array_last() need PHP 8.5. each() was removed in PHP 8.0. Run echo PHP_VERSION; to check your server.
How to Call a PHP Array Function
Every array helper is a global function—call it directly. The array is usually the first argument, but a few functions such as array_map() and in_array() put the callback or the needle first. Sort and stack functions change the array you pass in; most others return a new one:
$fruits = ['banana', 'apple', 'cherry'];
echo count($fruits); // 3
var_dump(in_array('apple', $fruits, true)); // bool(true)
sort($fruits); // changes $fruits in place
echo implode(', ', $fruits); // apple, banana, cherry
$upper = array_map('strtoupper', $fruits); // returns a new array
echo implode(', ', $upper); // APPLE, BANANA, CHERRY
$big = array_filter([4, 9, 15], fn($n) => $n > 5);
echo implode(', ', $big); // 9, 15
$all = array_merge($fruits, ['date']);
echo implode(', ', $all); // apple, banana, cherry, date PHP Array Functions Table
All function guides in this PHP tutorial series (88 tutorials in 12 categories).
Create & build
7 functionsMake a new array from literals, number ranges, variables, or a pair of lists. The short syntax [] is the same as array().
| Function | Description | Tutorial |
|---|---|---|
array() | Create an array from a list of values or key => value pairs. | Try it |
range() | Create an array of numbers or letters between a start and an end. | Try it |
compact() | Create an array from variables, using their names as keys. | Try it |
array_fill() | Fill an array with one value, starting at a given index. | Try it |
array_fill_keys() | Create an array with the given keys and the same value for each. | Try it |
array_combine() | Create an array using one array for keys and another for values. | Try it |
array_pad() | Pad an array to a given length with a value. | Try it |
Inspect & search
13 functionsCount elements, look for values and keys, list keys and values, and read the first or last item without changing the array.
| Function | Description | Tutorial |
|---|---|---|
count() | Count the elements in an array. | Try it |
sizeof() | Alias of count(): count the elements in an array. | Try it |
in_array() | Check whether a value exists in an array. | Try it |
array_search() | Search for a value and return its key, or false. | Try it |
array_key_exists() | Check whether a key exists in an array. | Try it |
key_exists() | Alias of array_key_exists(): check whether a key exists. | Try it |
array_is_list() | Check whether the keys are 0, 1, 2 in order (PHP 8.1+). | Try it |
array_keys() | Return all keys, or only the keys that match a value. | Try it |
array_values() | Return all values with the keys renumbered from 0. | Try it |
array_key_first() | Get the first key of an array. | Try it |
array_key_last() | Get the last key of an array. | Try it |
array_first() | Get the first value of an array (PHP 8.5+). | Try it |
array_last() | Get the last value of an array (PHP 8.5+). | Try it |
Transform
14 functionsTurn an array into a different one: change each value, keep some, cut pieces out, reverse, remove duplicates, or swap keys and values.
| Function | Description | Tutorial |
|---|---|---|
array_map() | Apply a callback to every element and return the results. | Try it |
array_filter() | Keep only the elements that pass a callback test. | Try it |
array_reduce() | Reduce an array to a single value with a callback. | Try it |
array_walk() | Run a callback on every element, changing it in place if needed. | Try it |
array_walk_recursive() | Run a callback on every element of nested arrays. | Try it |
array_column() | Return one column of values from an array of rows. | Try it |
array_chunk() | Split an array into chunks of a given size. | Try it |
array_slice() | Extract a piece of an array without changing the original. | Try it |
array_splice() | Remove or replace a piece of an array in place. | Try it |
array_reverse() | Return the array in reverse order. | Try it |
array_unique() | Remove duplicate values from an array. | Try it |
array_flip() | Swap the keys and values of an array. | Try it |
array_change_key_case() | Change all string keys to lower or upper case. | Try it |
array_count_values() | Count how many times each value appears. | Try it |
Math & random
3 functionsAdd up or multiply the values of an array, or pick random keys from it.
| Function | Description | Tutorial |
|---|---|---|
array_sum() | Calculate the sum of the values in an array. | Try it |
array_product() | Calculate the product of the values in an array. | Try it |
array_rand() | Pick one or more random keys from an array. | Try it |
Stack & queue (mutate)
4 functionsAdd or remove items at the end or the front of an array. All four functions change the array you pass in.
| Function | Description | Tutorial |
|---|---|---|
array_push() | Add one or more elements to the end of an array. | Try it |
array_pop() | Remove and return the last element of an array. | Try it |
array_shift() | Remove and return the first element of an array. | Try it |
array_unshift() | Add one or more elements to the front of an array. | Try it |
Merge & replace
4 functionsJoin arrays into one, or overwrite values of one array with values from another. The recursive versions go through nested arrays.
| Function | Description | Tutorial |
|---|---|---|
array_merge() | Merge one or more arrays into a single array. | Try it |
array_merge_recursive() | Merge arrays and combine values that share a key into nested arrays. | Try it |
array_replace() | Replace values of the first array with values from later arrays. | Try it |
array_replace_recursive() | Replace values of nested arrays, level by level. | Try it |
Diff (compare)
8 functionsKeep what is in the first array but not in the others. The plain version compares values; _assoc adds keys, _key compares keys only, and u versions use your own comparison function.
| Function | Description | Tutorial |
|---|---|---|
array_diff() | Compute the values in the first array that are missing from the others. | Try it |
array_diff_assoc() | Compute the difference, comparing both keys and values. | Try it |
array_diff_key() | Compute the difference, comparing keys only. | Try it |
array_diff_uassoc() | Compute the difference with a callback to compare keys. | Try it |
array_diff_ukey() | Compute the difference of keys with a callback. | Try it |
array_udiff() | Compute the difference of values with a callback. | Try it |
array_udiff_assoc() | Compute the difference with a callback for values and a plain key check. | Try it |
array_udiff_uassoc() | Compute the difference with callbacks for both values and keys. | Try it |
Intersect (common)
8 functionsKeep what appears in every array. It follows the same naming as the diff family: _assoc, _key, and u variants.
| Function | Description | Tutorial |
|---|---|---|
array_intersect() | Compute the values that are present in all arrays. | Try it |
array_intersect_assoc() | Compute the intersection, comparing both keys and values. | Try it |
array_intersect_key() | Compute the intersection, comparing keys only. | Try it |
array_intersect_uassoc() | Compute the intersection with a callback to compare keys. | Try it |
array_intersect_ukey() | Compute the intersection of keys with a callback. | Try it |
array_uintersect() | Compute the intersection of values with a callback. | Try it |
array_uintersect_assoc() | Compute the intersection with a callback for values and a plain key check. | Try it |
array_uintersect_uassoc() | Compute the intersection with callbacks for both values and keys. | Try it |
Sort
13 functionsOrder an array in place. A leading r reverses, a keeps keys with values, k sorts by key, and u uses your own comparison function.
| Function | Description | Tutorial |
|---|---|---|
sort() | Sort values from low to high and renumber the keys. | Try it |
rsort() | Sort values from high to low and renumber the keys. | Try it |
asort() | Sort values from low to high and keep the keys. | Try it |
arsort() | Sort values from high to low and keep the keys. | Try it |
ksort() | Sort by key from low to high. | Try it |
krsort() | Sort by key from high to low. | Try it |
natsort() | Sort with natural order, so item2 comes before item10. | Try it |
natcasesort() | Sort with natural order, ignoring case. | Try it |
usort() | Sort values with your own comparison function. | Try it |
uasort() | Sort values with your own function and keep the keys. | Try it |
uksort() | Sort by key with your own comparison function. | Try it |
array_multisort() | Sort several arrays, or a multi-dimensional array, together. | Try it |
shuffle() | Put the elements of an array in random order. | Try it |
Pointer & legacy
8 functionsMove an internal pointer through an array by hand. These are older tools; prefer foreach in new code. each() was removed in PHP 8.0.
| Function | Description | Tutorial |
|---|---|---|
current() | Return the element the internal pointer points to. | Try it |
pos() | Alias of current(): return the current element. | Try it |
key() | Return the key the internal pointer points to. | Try it |
next() | Move the pointer forward and return that element. | Try it |
prev() | Move the pointer back and return that element. | Try it |
reset() | Move the pointer to the first element and return it. | Try it |
end() | Move the pointer to the last element and return it. | Try it |
each() | Return the current key and value, then advance (removed in PHP 8.0). | Try it |
Unpack
2 functionsSpread the values of an array into separate variables.
PHP 8.4+ find helpers
4 functionsCallback helpers that replace a common foreach loop. They need PHP 8.4 or newer.
| Function | Description | Tutorial |
|---|---|---|
array_find() | Return the first value that passes a callback test. | Try it |
array_find_key() | Return the key of the first value that passes a callback test. | Try it |
array_any() | Check whether at least one element passes a callback test. | Try it |
array_all() | Check whether every element passes a callback test. | Try it |
Frequently Asked Questions
Start with array()
Open the first guide—syntax, examples, and FAQs.
6 people found this page helpful
