Skip to main content

PHP Array Functions

PHP ships with a large set of built-in functions for working with arrays.

These are the ones you are most likely to reach for:

  • array() - Builds a new array
  • array_change_key_case() - Converts every key to lowercase or uppercase
  • array_chunk() - Breaks an array into smaller arrays, or chunks
  • array_column() - Picks out the values of one column from the input array
  • array_combine() - Builds an array from two others, one supplying the keys and the other the values
  • array_count_values() - Counts how many times each value appears
  • array_diff() - Compares arrays and gives back the differences, looking at values only
  • array_diff_assoc() - Compares arrays and gives back the differences, looking at both keys and values
  • array_diff_key() - Compares arrays and gives back the differences, looking at keys only
  • array_fill() - Populates an array with a value you choose
  • array_fill_keys() - Populates an array with a value, using the keys you list
  • array_filter() - Keeps only the values that a callback function accepts
  • array_flip() - Swaps each key with its value
  • array_intersect() - Compares arrays and gives back what they have in common, looking at values only
  • array_intersect_assoc() - Compares arrays and gives back what they have in common, looking at both keys and values
  • array_intersect_key() - Compares arrays and gives back what they have in common, looking at keys only
  • array_key_exists() - Tells you whether a given key is in the array
  • array_keys() - Gives back all of an array's keys
  • array_map() - Passes each value to a function you write and collects the new values it returns
  • array_merge() - Combines one or more arrays into a single array
  • array_merge_recursive() - Combines one or more arrays into a single array, working recursively
  • array_multisort() - Sorts several arrays at once, or a multidimensional array
  • array_pad() - Extends an array to a given length, filling the new items with a value you specify
  • array_pop() - Takes the last element off the array
  • array_product() - Multiplies all the values together
  • array_push() - Appends one or more elements at the end
  • array_rand() - Picks one or more keys at random
  • array_reduce() - Reduces an array to a single value with a function you write
  • array_replace() - Overwrites the first array's values with those from the arrays after it
  • array_replace_recursive() - Overwrites the first array's values with those from the arrays after it, working recursively
  • array_reverse() - Gives back the array with its order reversed
  • array_search() - Looks for a value and gives back its key
  • array_shift() - Takes the first element off the array and gives it back to you
  • array_slice() - Gives back a selected part of an array
  • array_splice() - Cuts out chosen elements and can put others in their place
  • array_sum() - Adds up all the values
  • array_unique() - Drops duplicate values
  • array_unshift() - Places one or more elements at the start
  • array_values() - Gives back all of an array's values
  • array_walk() - Runs a function you write on every element
  • array_walk_recursive() - Runs a function you write on every element, working recursively
  • arsort() - Sorts an associative array by value, high to low
  • asort() - Sorts an associative array by value, low to high
  • compact() - Builds an array from variables and their values
  • count() - Tells you how many elements an array has
  • current() - Gives back the element the internal pointer is on
  • end() - Moves the internal pointer to the last element
  • extract() - Turns array entries into variables in the current symbol table
  • in_array() - Tells you whether a given value is in the array
  • key() - Gives back the key at the internal pointer's position
  • krsort() - Sorts an associative array by key, high to low
  • ksort() - Sorts an associative array by key, low to high
  • list() - Assigns values from an array to several variables in one step
  • natcasesort() - Sorts with a natural order algorithm, ignoring case
  • natsort() - Sorts with a natural order algorithm
  • next() - Moves the internal pointer forward one element
  • pos() - Another name for current()
  • prev() - Moves the internal pointer back one element
  • range() - Builds an array from a range of elements
  • reset() - Moves the internal pointer to the first element
  • rsort() - Sorts an indexed array, high to low
  • shuffle() - Puts the elements in random order
  • sizeof() - Another name for count()
  • sort() - Sorts an indexed array, low to high
  • uasort() - Sorts by value with a comparison function you write, keeping each key attached to its value
  • uksort() - Sorts by key with a comparison function you write
  • usort() - Sorts by value with a comparison function you write