PHP Array Functions

Beginner
6 min read
Updated: Nov 2026
88 function guides
Function index

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().

Beginner path.

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().

Mind your PHP version.

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:

PHP
$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 functions

Make a new array from literals, number ranges, variables, or a pair of lists. The short syntax [] is the same as array().

FunctionDescriptionTutorial
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

Transform

14 functions

Turn an array into a different one: change each value, keep some, cut pieces out, reverse, remove duplicates, or swap keys and values.

FunctionDescriptionTutorial
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 functions

Add up or multiply the values of an array, or pick random keys from it.

FunctionDescriptionTutorial
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 functions

Add or remove items at the end or the front of an array. All four functions change the array you pass in.

FunctionDescriptionTutorial
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 functions

Join arrays into one, or overwrite values of one array with values from another. The recursive versions go through nested arrays.

FunctionDescriptionTutorial
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 functions

Keep 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.

FunctionDescriptionTutorial
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 functions

Keep what appears in every array. It follows the same naming as the diff family: _assoc, _key, and u variants.

FunctionDescriptionTutorial
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 functions

Order an array in place. A leading r reverses, a keeps keys with values, k sorts by key, and u uses your own comparison function.

FunctionDescriptionTutorial
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 functions

Move an internal pointer through an array by hand. These are older tools; prefer foreach in new code. each() was removed in PHP 8.0.

FunctionDescriptionTutorial
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 functions

Spread the values of an array into separate variables.

FunctionDescriptionTutorial
list()Assign array values to variables in one statement.Try it
extract()Create variables from the keys of an array.Try it

PHP 8.4+ find helpers

4 functions

Callback helpers that replace a common foreach loop. They need PHP 8.4 or newer.

FunctionDescriptionTutorial
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

They are the built-in functions that create, search, change, combine, compare, and sort arrays. Most of them are always available because they belong to PHP's core, so you do not need to install or enable anything.
The Array page teaches the ideas: lists versus associative arrays, callbacks, merging, sorting, and the newer PHP 8.x helpers, with fixed examples. This index lists every function tutorial so you can open array_map(), array_merge(), sort(), and the rest.
Start with count() and in_array() to inspect an array, then array_push(), array_pop(), array_shift() and array_unshift() to change it. After that learn array_map(), array_filter() and array_reduce(), then array_merge() and the sort family.
The sort functions, array_push(), array_pop(), array_shift(), array_unshift(), array_splice(), array_walk(), shuffle(), and the pointer functions work on the array by reference. Functions such as array_map(), array_filter(), array_merge(), array_slice(), array_reverse(), and array_unique() return a new array and leave the original alone.
The plain versions compare values only. The _assoc versions compare keys and values, and the _key versions compare keys only. A leading u (array_udiff, array_uintersect) lets you pass your own function to compare values, and uassoc and ukey do the same for keys. The same naming pattern applies to both families.
No. array_find(), array_find_key(), array_any(), and array_all() were added in PHP 8.4, and array_first() and array_last() in PHP 8.5. array_is_list() needs PHP 8.1. On older versions use a foreach loop or array_filter() for the same job.
No. each() was deprecated in PHP 7.2 and removed in PHP 8.0. Use foreach, or the pointer functions current(), key(), next(), prev(), reset() and end() when you must move through an array by hand. Its guide is kept in the index to help you read and update old code.

Start with array()

Open the first guide—syntax, examples, and FAQs.

array() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful