English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Array Function Manual

PHP Array FunctionsAllows you to interact with and operate arrays in various ways. PHP arrays are crucial for storing, managing, and operating sets of variables.

PHP supports simple multi-dimensional arrays, which can be created by users or other functions.

There is no need to install PHP array functions; array functions are part of the PHP core, so you can use these functions in your scripts without further installation.

PHP Array Functions

The following table lists all functions related to PHP Array. The version in the table indicates the earliest version of PHP that supports the function.

FunctionFunction Description
array()Create an array.
array_change_key_case()Return an array whose keys are all uppercase or lowercase.
array_chunk()Split an array into new array blocks.
array_column()Return the values of a single column from the input array.
array_combine()Create a new array by merging two arrays (one for key names and one for key values).
array_count_values()Used to count the number of times all values appear in the array.
array_diff()Compare arrays and return the difference set of the two arrays (only compare key values).
array_diff_assoc()Compare arrays and return the difference set of the two arrays (compare key names and key values).
array_diff_key()Compare arrays and return the difference set of the two arrays (only compare key names).
array_diff_uassoc()Compare arrays and return the difference set of the two arrays (compare key names and key values, using a user-defined key name comparison function).
array_diff_ukey()Compare arrays and return the difference set of the two arrays (only compare key names, using a user-defined key name comparison function).
array_fill()Fill an array with values using the given keys.
array_fill_keys()Fill an array with values using the specified key names.
array_filter()Filter elements in the array using a callback function.
array_flip()Reverse/Swap the key names and corresponding associated key values in the array.
array_intersect()Compare arrays and return the intersection of the two arrays (only compare key values).
array_intersect_assoc()Compare arrays and return the intersection of the two arrays (compare key names and key values).
array_intersect_key()Compare arrays and return the intersection of the two arrays (only compare key names).
array_intersect_uassoc()Compare arrays and return the intersection of the two arrays (compare key names and key values, use a user-defined key comparison function).
array_intersect_ukey()Compare arrays and return the intersection of the two arrays (only compare key names, use a user-defined key comparison function).
array_key_exists()Check if the specified key name exists in the array.
array_keys()Return all the key names in the array.
array_map()Apply a user-defined function to each value in the given array, and return the new values.
array_merge()Merge one or more arrays into a single array.
array_merge_recursive()Recursively merge one or more arrays into a single array.
array_multisort()Sort multiple arrays or multidimensional arrays.
array_pad()Insert a specified number of elements with a specified value into the array.
array_pop()Delete the last element of the array (pop).
array_product()Calculate the product of all values in the array.
array_push()Insert one or more elements at the end of the array (push).
array_rand()Randomly select one or more elements from the array, and return the key name.
array_reduce()Iteratively reduce an array to a string using a user-defined function, and return it.
array_replace()Replace the values of the first array with the values of the subsequent array.
array_replace_recursive()Recursively use the values of the subsequent array to replace the values of the first array.
array_reverse()Reverse the order of elements in the original array, create a new array, and return it.
array_search()Search for the given value in the array, and return the corresponding key name if successful.
array_shift()Delete the first element of the array and return the value of the deleted element.
array_slice()Return a selected part of the array.
array_splice()Remove the specified element from the array and replace it with other values.
array_sum()Return the sum of all values in the array.
array_udiff()Compare arrays and return the difference set of the two arrays (compare only key values using a user-defined key name comparison function).
array_udiff_assoc()Compare arrays and return the difference set of the two arrays (compare key names and key values using built-in functions for key names and user-defined functions for key values).
array_udiff_uassoc()Compare arrays and return the difference set of the two arrays (compare key names and key values using two user-defined key name comparison functions).
array_uintersect()Compare arrays and return the intersection of the two arrays (compare only key values using a user-defined key name comparison function).
array_uintersect_assoc()Compare arrays and return the intersection of the two arrays (compare key names and key values using built-in functions for key names and user-defined functions for key values).
array_uintersect_uassoc()Compare arrays and return the intersection of the two arrays (compare key names and key values using two user-defined key name comparison functions).
array_unique()Remove duplicate values from the array.
array_unshift()Insert one or more elements at the beginning of the array.
array_values()Return all values in the array.
array_walk()Apply a user-defined function to each member of the array.
array_walk_recursive()Apply a user-defined function recursively to each member of the array.
arsort()Sort the associative array in descending order by key value.
asort()Sort the associative array in ascending order by key value.
compact()Create an array containing variable names and their values.
count()Return the number of elements in the array.
current()Return the current element in the array.
each()Return the current key/value pair in the array.
end()Move the internal pointer of the array to the last element.
extract()Import variables from the array into the current symbol table.
in_array()Check if a specified value exists in the array.
key()Get the key name from the associative array.
krsort()Sort the associative array in descending order by key name.
ksort()Sort the associative array in ascending order by key name.
list()Assign the values in the array to some array variables.
natcasesort()Sort an array using the 'natural sort' algorithm without case sensitivity.
natsort()Sort an array using the 'natural sort' algorithm.
next()Move the internal pointer of the array forward one position.
pos()Alias of current().
prev()Move the internal pointer of the array back one position.
range()Create an array containing elements of a specified range.
reset()Set the internal pointer of the array to the first element.
rsort()Sort an array of numbers in descending order.
shuffle()Rearrange the elements of an array in a random order.
sizeof()Alias of count().
sort()Sort an array of numbers in ascending order.
uasort()Sort the values of an array using a user-defined comparison function.
uksort()Sort the keys of an array using a user-defined comparison function.
usort()Sort an array using a user-defined comparison function.

PHP array constants

NumberConstants and descriptions
1

CASE_LOWER

Used in conjunction with array_change_key_case() to convert array keys to lowercase.

2

CASE_UPPER

Used in conjunction with array_change_key_case() to convert array keys to uppercase.

3

SORT_ASC

Used in conjunction with array_multisort() to sort in ascending order.

4

SORT_DESC

Used in conjunction with array_multisort() to sort in descending order.

5

SORT_REGULAR

Default. Sort each item in regular order. (Sorting is done based on the original type.)

6

SORT_NUMERIC

Sort each item in numerical order. (Strings are also treated as numbers during sorting, for example:11<100)

7

SORT_STRING

 Sort each item in alphabetical order. (Numbers are also treated as strings during sorting, for example:100<11)

8

SORT_LOCALE_STRING

Used to compare items as strings based on the current locale.

9

COUNT_NORMAL

10

COUNT_RECURSIVE

11

EXTR_OVERWRITE

12

EXTR_SKIP

13

EXTR_PREFIX_SAME

14

EXTR_PREFIX_ALL

15

EXTR_PREFIX_INVALID

16

EXTR_PREFIX_IF_EXISTS

17

EXTR_IF_EXISTS

18

EXTR_REFS