PHP - Arrays
These array functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables. Simple and multi-dimensional arrays are also supported.
Installation
There is no installation needed to use these functions. These functions are part of the PHP core.
Runtime Configuration
This extension has no configuration directives defined in php.ini.
PHP Array Functions
Functions | Description |
---|---|
array() | Creates an array. |
array_change_key_case() | Changes all keys of an array in either lowercase or uppercase. |
array_chunk() | Splits an array into chunks of arrays. |
array_column() | Returns the values from a single column in the input array. |
array_combine() | Creates an array by using one array for keys and another for its values. |
array_count_values() | Counts all the values of an array. |
array_diff() | Compare arrays, and returns the differences (compare values only). |
array_diff_assoc() | Compare arrays, and returns the differences (compare keys and values). |
array_diff_key() | Compare arrays, and returns the differences (compare keys only). |
array_diff_uassoc() | Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function). |
array_diff_ukey() | Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function). |
array_fill() | Fills an array with specified values. |
array_fill_keys() | Fills an array with values, specifying keys. |
array_filter() | Filters the values of an array using a callback function. |
array_flip() | Flips/Exchanges all keys with their associated values in an array. |
array_intersect() | Compare arrays, and returns the matches (compare values only). |
array_intersect_assoc() | Compare arrays and returns the matches (compare keys and values). |
array_intersect_key() | Compare arrays, and returns the matches (compare keys only). |
array_intersect_uassoc() | Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function). |
array_intersect_ukey() | Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function). |
array_key_exists() | Checks if the specified key exists in the array. |
array_key_first() | Gets the first key of an array. |
array_key_last() | Gets the last key of an array. |
array_keys() | Returns all the keys of an array. |
array_map() | Sends each value of an array to a user-made function, which returns new values. |
array_merge() | Merges one or more arrays into one array. |
array_merge_recursive() | Merges one or more arrays into one array recursively. |
array_multisort() | Sorts multiple or multi-dimensional arrays. |
array_pad() | Pad array to the specified length with a value. |
array_pop() | Deletes the last element of an array. |
array_product() | Returns the product of all values in an array. |
array_push() | Inserts one or more elements to the end of an array. |
array_rand() | Returns one or more random keys from an array. |
array_reduce() | Iteratively reduce the array to a single value using a callback function. |
array_replace() | Replaces elements from passed arrays into the first array. |
array_replace_recursive() | Replaces the values of the first array with the values from following arrays recursively. |
array_reverse() | Returns an array in the reverse order. |
array_search() | Searches an array for a given value and returns the key. |
array_shift() | Removes the first element from an array, and returns the value of the removed element. |
array_slice() | Returns selected parts of an array. |
array_splice() | Removes and replaces specified elements of an array. |
array_sum() | Returns sum of all values in the array. |
array_udiff() | Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function). |
array_udiff_assoc() | Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values). |
array_udiff_uassoc() | Compare arrays, and returns the differences (compare keys and values, using two user-defined key comparison functions). |
array_uintersect() | Compare arrays, and returns the matches (compare values only, using a user-defined key comparison function). |
array_uintersect_assoc() | Compare arrays, and returns the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values). |
array_uintersect_uassoc() | Compare arrays, and returns the matches (compare keys and values, using two user-defined key comparison functions). |
array_unique() | Removes duplicate values from an array. |
array_unshift() | Adds one or more elements to the beginning of an array. |
array_values() | Returns all the values of an array. |
array_walk() | Applies a user function to every member of an array. |
array_walk_recursive() | Applies a user function recursively to every member of an array. |
arsort() | Sorts an array in descending order, according to the value and maintaining key to value association. |
asort() | Sorts an array in ascending order, according to the value and maintaining key to value association. |
compact() | Create array containing variables and their values. |
count() | Returns the number of elements in an array. |
current() | Returns the current element in an array. |
each() | Returns the current key and value pair from an array. Deprecated from PHP 7.2. |
end() | Sets the internal pointer of an array to its last element. |
extract() | Imports variables into the current symbol table from an array. |
in_array() | Checks if a specified value exists in an array. |
key() | Fetches a key from an array. |
key_exists() | Checks if the specified key exists in the array. Alias of array_key_exists() function. |
krsort() | Sorts an associative array in descending order, according to the key and maintaining key to value association. |
ksort() | Sorts an associative array in ascending order, according to the key and maintaining key to value association. |
list() | Assigns variables as if they were an array. |
natcasesort() | Sorts an array using a case insensitive "natural order" algorithm. |
natsort() | Sorts an array using a "natural order" algorithm. |
next() | Advance the internal array pointer of an array. |
pos() | Returns the current element in an array. Alias of current() function. |
prev() | Rewinds the internal array pointer. |
range() | Creates an array containing a range of elements. |
reset() | Sets the internal pointer of an array to its first element. |
rsort() | Sorts an array in descending order. |
shuffle() | Shuffles an array. |
sizeof() | Returns the number of elements in an array. Alias of count() function. |
sort() | Sorts an array in ascending order. |
uasort() | Sorts an associative array using a user-defined comparison function, according to the value. |
uksort() | Sorts an associative array using a user-defined comparison function, according to the key. |
usort() | Sorts an array using a user-defined comparison function. |
PHP Array Predefined Constants
The constants below are always available as part of the PHP core.
Constant | Type | Description |
---|---|---|
CASE_LOWER | Integer | Used with array_change_key_case() and is used to convert array keys to lower case. Also the default case for array_change_key_case(). |
CASE_UPPER | Integer | Used with array_change_key_case() and is used to convert array keys to upper case. |
Sorting order flags | ||
SORT_ASC | Integer | Used with array_multisort() to sort in ascending order. |
SORT_DESC | Integer | Used with array_multisort() to sort in descending order. |
Sorting type flags | ||
SORT_REGULAR | Integer | Used to compare items normally. |
SORT_NUMERIC | Integer | Used to compare items numerically. |
SORT_STRING | Integer | Used to compare items as strings. |
SORT_LOCALE_STRING | Integer | Used to compare items as strings, based on the current locale. |
SORT_NATURAL | Integer | Used to compare items as strings using "natural ordering" like natsort(). |
SORT_FLAG_CASE | Integer | It can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively. |
Filter flags | ||
ARRAY_FILTER_USE_KEY | Integer | Used with array_filter() to pass each key as the first argument to the given callback function. |
ARRAY_FILTER_USE_BOTH | Integer | Used with array_filter() to pass both value and key to the given callback function. |
COUNT_NORMAL | Integer | |
COUNT_RECURSIVE | Integer | |
EXTR_OVERWRITE | Integer | |
EXTR_SKIP | Integer | |
EXTR_PREFIX_SAME | Integer | |
EXTR_PREFIX_ALL | Integer | |
EXTR_PREFIX_INVALID | Integer | |
EXTR_PREFIX_IF_EXISTS | Integer | |
EXTR_IF_EXISTS | Integer | |
EXTR_REFS | Integer |