1. php
  2. /references
  3. /array-functions

PHP Array Functions

PHP's built in Array Function provide a convenient way to perform operations on arrays, such as sorting, searching, filtering, and transforming the elements of an array.

Some common tasks that can be performed using PHP array functions include:

  • Sorting an array by its values or keys
  • Removing duplicate values from an array
  • Filtering an array to include only certain elements that meet certain criteria
  • Merging multiple arrays into a single array
  • Transforming the values of an array using a callback function
  • Searching for a specific value or key in an array
  • Counting the number of elements in an array

Here is a complete list of PHP's Array Functions:

Function nameDescription
array_change_key_caseChanges the case of all keys in an array.
array_chunkSplits an array into chunks of new arrays.
array_columnReturns the values from a single column of an input array, identified by the column_key.
array_combineCreates an array by using the values from one array as keys and the values from another array as the corresponding values.
array_count_valuesCounts all the values of an array.
array_diffComputes the difference of arrays.
array_diff_assocComputes the difference of arrays with additional index check.
array_diff_keyComputes the difference of arrays using keys for comparison.
array_diff_uassocComputes the difference of arrays with additional index check which is performed by a user supplied callback function.
array_diff_ukeyComputes the difference of arrays using a callback function on the keys for comparison.
array_fillFills an array with values.
array_fill_keysFills an array with values, specifying keys.
array_filterFilters the values of an array using a callback function.
array_flipFlips/exchanges all keys with their associated values in an array.
array_intersectComputes the intersection of arrays.
array_intersect_assocComputes the intersection of arrays with additional index check.
array_intersect_keyComputes the intersection of arrays using keys for comparison.
array_intersect_uassocComputes the intersection of arrays with additional index check, compares indexes by a callback function.
array_intersect_ukeyComputes the intersection of arrays using a callback function on the keys for comparison.
array_key_existsChecks if the given key or index exists in the array.
array_keysReturns all the keys or a subset of the keys of an array.
array_mapApplies a callback function to the elements of an array.
array_mergeMerges one or more arrays into one array.
array_merge_recursiveRecursively merges two or more arrays into one array.
array_multisortSorts multiple or multi-dimensional arrays.
array_padPad array to the specified length with a value.
array_popRemoves the last element of an array, and returns the value of the removed element.
array_productCalculates the product of the values in an array.
array_pushPushes one or more elements onto the end of an array.
array_randPicks one or more random entries out of an array.
array_reduceIteratively reduce the array to a single value using a callback function.
array_replaceReplaces elements from passed arrays into the first array recursively.
array_replace_recursiveReplaces elements from passed arrays into the first array recursively.
array_reverseReturns an array with elements in reverse order.
array_searchSearches the array for a given value and returns the corresponding key if successful.
array_shiftRemoves the first element of an array, and returns the value of the removed element.
array_sliceExtracts a slice of an array.
array_spliceRemoves and replaces specified elements of an array.
array_sumCalculates the sum of the values in an array.
array_udiffComputes the difference of arrays by using a callback function for data comparison.
array_uintersect_assocComputes the intersection of arrays with additional index check, compares data by a callback function.
array_uintersect_uassocComputes the intersection of arrays with additional index check, compares indexes by a callback function.
array_uniqueRemoves duplicate values from an array.
array_unshiftPrepends one or more elements to an array.
array_valuesReturns all the values of an array.
array_walkApply a user function to every member of an array.
array_walk_recursiveApply a user function recursively to every member of an array.
arsortSorts an array in reverse order and maintains index association.
asortSorts an array and maintains index association.
compactCreates an array containing variables and their values.
countCounts all elements in an array, or something in an object.
currentReturns the current element in an array.
eachReturns the current key and value pair from an array and advances the array cursor.
endSets the internal pointer of an array to its last element.
extractImports variables into the current symbol table from an array.
in_arraySearches an array for a specific value.
keyReturns the key of the element currently pointed to by the internal array pointer.
key_existsChecks if the given key or index exists in the array.
krsortSorts an array by key in reverse order.
ksortSorts an array by key.
natcasesortSorts an array using a case insensitive "natural order" algorithm.
natsortSorts an array using a "natural order" algorithm.
nextAdvances the internal pointer of an array to the next element, and returns the value of the current element after the increment.
posAlias of current().
prevRewinds the internal array pointer to the previous element and returns its value.
rangeCreates an array containing a range of elements.
resetRewinds array's internal pointer to the first element and returns the value of the first array element.
rsortSorts an array in reverse order.
shuffleShuffles an array.
sizeofAlias of count().
sortSorts an array.
uasortSorts an array with a user-defined comparison function and maintains index association.
uksortSorts an array by keys using a user-defined comparison function.
usortSorts an array by values using a user-defined comparison function.