PHP 8 reference · 49 entries

PHP Array Functions

PHP array functions: count(), array_map(), array_filter(), in_array(), array_merge(), array_column(), array_find() and more, with runnable examples.

PHP arrays are ordered maps: the same type serves as a list ([0 => "a", 1 => "b"]) and as a dictionary (["name" => "Ada"]). Watch the keys: some functions keep them (array_filter()), others renumber them (array_merge(), sort()). Functions marked PHP 8.4 or 8.5 show their output from that version, since the in-browser runner uses PHP 8.3.

NameDescriptionSince
count() Return the number of elements in an array or Countable object. With COUNT_RECURSIVE it also counts nested elements. 4.0
array_push() Add one or more elements to the end of an array and return its new length. $array[] = $value does the same for one value. 4.0
array_pop() Remove the last element of an array and return it. Returns null if the array is empty. 4.0
array_shift() Remove the first element of an array and return it. Numeric keys are renumbered from 0; string keys stay. 4.0
array_unshift() Add one or more elements to the start of an array and return its new length. Numeric keys are renumbered. 4.0
array_merge() Merge one or more arrays into a new array. Numeric keys are renumbered; for string keys the later value wins. 4.0
array_merge_recursive() Merge arrays and, when string keys clash, combine their values into arrays instead of overwriting. 4.0
array_combine() Create an array by using one array for keys and another for values. Both arrays must have the same number of elements. 5.0
array_slice() Return a portion of an array without changing the original, given an offset and an optional length. 4.0
array_splice() Remove a portion of an array and optionally replace it with other elements, changing the array in place. 4.0
array_map() Apply a callback to every element of one or more arrays and return a new array of the results. 4.0
array_filter() Keep only the elements for which a callback returns true. Without a callback it removes empty values; keys are kept. 4.0
array_reduce() Reduce an array to a single value by passing a running total and each element to a callback. 4.0
array_walk() Run a callback on every element of an array, with the value and key. Take the value by reference to change it in place. 4.0
array_keys() Return all the keys of an array, or only the keys whose value matches a search value. 4.0
array_values() Return all the values of an array as a list re-indexed from 0. Often used after array_filter() or unset(). 4.0
array_key_exists() Check whether an array has a given key, even if its value is null. isset() returns false for null values. 4.0
in_array() Check whether a value exists in an array. Pass true as the third argument for a strict (===) comparison. 4.0
array_search() Search an array for a value and return the first matching key, or false if it isn't there. 4.0
array_key_first() Return the first key of an array without moving its internal pointer, or null for an empty array. Added in 7.3. 7.3
array_key_last() Return the last key of an array without moving its internal pointer, or null for an empty array. Added in 7.3. 7.3
array_is_list() Check whether an array's keys are 0, 1, 2… in order, so it would encode as a JSON array. Added in PHP 8.1. 8.1
array_find() Return the first element for which a callback returns true, or null if none match. Added in PHP 8.4. 8.4
array_find_key() Return the key of the first element for which a callback returns true, or null if none match. Added in PHP 8.4. 8.4
array_any() Return true if at least one element passes a callback test, stopping at the first match. Added in PHP 8.4. 8.4
array_all() Return true if every element passes a callback test, stopping at the first failure. Added in PHP 8.4. 8.4
array_first() Return the first value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5. 8.5
array_last() Return the last value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5. 8.5
array_unique() Remove duplicate values from an array, keeping the first occurrence of each and its key. 4.0
array_reverse() Return a new array with the elements in reverse order. String keys are kept; numeric keys are renumbered by default. 4.0
array_flip() Swap keys and values, so each value becomes a key. Values must be strings or integers; duplicates keep the last key. 4.0
array_fill() Create an array filled with the same value, given a start index and a count. 4.2
array_fill_keys() Create an array from a list of keys, giving every key the same value. Useful for defaults and counters. 5.2
range() Create an array of numbers or letters from a start to an end value, with an optional step, such as range(1, 10, 2). 4.0
array_column() Return the values from one column of a list of records, optionally keyed by another column. 5.5
array_chunk() Split an array into smaller arrays of a given size. The last chunk may be smaller. 4.2
array_pad() Pad an array to a given length with a value, adding at the end (positive length) or the start (negative). 4.0
array_sum() Return the sum of the values in an array. Numeric strings are converted; an empty array gives 0. 4.0
array_product() Return the product of the values in an array, multiplying them all together. An empty array gives 1. 5.1
array_count_values() Count how many times each value occurs in an array, returning value => count pairs. 4.0
array_diff() Return the values of the first array that are not in any of the others. Values are compared as strings. 4.0
array_diff_key() Return the entries of the first array whose keys are not in any of the other arrays. 5.1
array_diff_assoc() Return the entries of the first array whose key and value pair isn't in any of the other arrays. 4.3
array_intersect() Return the values of the first array that are also present in all the other arrays. Keys come from the first array. 4.0
array_intersect_key() Return the entries of the first array whose keys exist in all the other arrays. Great for whitelisting fields. 5.1
array_replace() Replace values of the first array with values that have the same keys in later arrays. Numeric keys are kept. 5.3
array_rand() Pick one or more random keys from an array. Use it with the array to get random values; not for security. 4.0
compact() Create an array from variable names, using each name as the key and the variable's value as the value. 4.0
extract() Create variables from an array's keys and values in the current scope. Never use it on user input. 4.0

Was this page helpful?