Array Functions in PHP - GeeksforGeeks

Array Functions in PHP

Last Updated : 26 Oct, 2026

PHP provides built-in array functions to create, access, search, filter, compare, merge, and sort arrays. These functions reduce the need for manual loops and make array operations easier to maintain.

  • Array Manipulation: Add, remove, replace, merge, and extract array elements.
  • Searching and Filtering: Find values or keys, filter elements, and compare arrays.
  • Sorting and Iteration: Sort arrays by keys or values and process elements using built-in functions.
FunctionDescription
array() Creates an array.
array_all()Checks whether all elements satisfy a callback condition .
array_any()Checks whether at least one element satisfies a callback condition .
array_change_key_case()Converts string keys to lowercase or uppercase.
array_chunk()Splits an array into smaller arrays.
array_column()Extracts values from a specified column of a multidimensional array.
array_combine()Creates an array using one array for keys and another for values.
array_count_values() Counts the occurrences of integer and string values.
array_diff() Returns values from the first array that are absent from the other arrays.
array_diff_assoc()Compares arrays using both keys and values.
array_diff_keys()Compares arrays using keys.
array_diff_uassoc()Compares values normally and keys using a callback function.
array_diff_ukey()Compares array keys using a callback function.
array_fill()Creates an array with a specified number of elements containing a given value.
array_fill_keys()Creates an array using specified keys and a common value.
array_filter() Filters array elements using a callback function.
array_find()Returns the first element that satisfies a callback condition .
array_find_key()Returns the key of the first element that satisfies a callback condition .
array_flip()Swaps array keys and values.
array_intersect()Returns values common to all specified arrays.
array_intersect_assoc()Returns matching key-value pairs across arrays.
array_intersect_key()Returns elements whose keys exist in all specified arrays.
array_intersect_uassoc()Compares values normally and keys using a callback function.
array_intersect_ukey()Finds matching keys using a callback function.
array_is_list()Checks whether an array has consecutive integer keys starting from zero .
array_key_exists()Checks whether a specified key exists, even if its value is null.
array_keys()Returns all keys or keys matching a specified value.
array_map()Applies a callback function to array elements.
array_merge()Merges one or more arrays.
array_merge_recursive()Recursively merges arrays, combining values with matching string keys into arrays.
array_multisort()Sorts multiple arrays or a multidimensional array.
array_pad()Pads an array to a specified length with a given value.
array_pop()Removes and returns the last element of an array.
array_product()Calculates the product of array values.
array_push()Adds one or more elements to the end of an array.
array_rand() Returns one or more random keys from an array.
array_reduce() Reduces an array to a single value using a callback function.
array_replace()Replaces array values using values from other arrays.
array_replace_recursive()Recursively replaces values in nested arrays.
array_reverse()Reverses the order of array elements.
array_search() Searches for a value and returns its key if found.
array_shift()Removes and returns the first element of an array.
array_slice()Extracts a portion of an array without modifying the original.
array_splice()Removes or replaces a portion of an array.
array_sum()Calculates the sum of array values.
array_udiff()Compares array values using a callback function to find differences.
array_udiff_assoc()Compares values using a callback and checks key associations.
array_udiff_uassoc()Compares both keys and values using callback functions to find differences.
array_uintersect() Finds common values using a callback function.
array_uintersect_assoc()Finds common values using a callback and checks key associations.
array_uintersect_uassoc() Compares both keys and values using callback functions to find intersections.
array_unique()Removes duplicate values while preserving the first occurrence.
array_unshift() Adds one or more elements to the beginning of an array.
array_values() Returns all values from an array with numeric keys.
array_walk()Applies a callback function to each array element.
array_walk_recursive()Applies a callback function to elements in nested arrays.
arsort() Sorts an array by values in descending order while preserving key associations.
asort()Sorts an array by values in ascending order while preserving key associations.
compact()Creates an array from variables and their values.
count() Returns the number of elements in an array or a Countable object.
current() Returns the current element using the array's internal pointer.
end()Moves the internal pointer to the last element and returns its value.
extract()Imports array elements into the current symbol table as variables.
in_array()Checks whether a value exists in an array.
key() Returns the key of the current array element.
krsort() Sorts an array by keys in descending order.
ksort()Sorts an array by keys in ascending order.
list()Assigns array values to variables.
natcasesort()Sorts values using case-insensitive natural ordering while preserving keys.
natsort()Sorts values using natural ordering while preserving keys.
next()Advances the internal pointer and returns the next element.
prev()Moves the internal pointer backward and returns the previous element.
range()Creates an array containing a sequence of values.
reset()Moves the internal pointer to the first element and returns its value.
rsort()Sorts an array in descending order and reindexes numeric keys.
shuffle()Randomizes array element order and reindexes numeric keys.
sort()Sorts an array in ascending order and reindexes numeric keys.
uasort()Sorts an array by values using a callback while preserving key associations.
uksort()Sorts array keys using a callback function.
usort()Sorts array values using a callback and reindexes numeric keys.

Common PHP Array Functions

PHP provides several array functions for everyday operations, including adding and removing elements, sorting values, and checking array contents.

1. Adding and Removing Elements

  • array_push(): Adds one or more elements to the end of an array.
  • array_pop(): Removes and returns the last element.
  • array_shift(): Removes and returns the first element. Numeric keys are reindexed.
  • array_unshift(): Adds one or more elements to the beginning of an array.
PHP
<?php
$fruits = ["Apple", "Banana"];

array_push($fruits, "Cherry", "Date");
print_r($fruits);

array_pop($fruits);
print_r($fruits);

array_shift($fruits);
print_r($fruits);

array_unshift($fruits, "Orange");
print_r($fruits);
?>

2. Array Sorting

PHP provides different sorting functions depending on whether you want to sort an array by values or keys and whether the original key associations should be preserved.

  • sort(): Sorts an array in ascending order and resets its numeric keys.
  • asort(): Sorts an array by values in ascending order while preserving key associations.
  • ksort(): Sorts an array by keys in ascending order.
PHP
<?php
$numbers = [3, 1, 2, 5, 4];

sort($numbers);
print_r($numbers);

$person = ["GFG" => 30, "Ken" => 25, "Alen" => 35];

asort($person);
print_r($person);

ksort($person);
print_r($person);
?>

Benefits

  • Simplifies Array Operations: Built-in functions handle tasks such as sorting, filtering, searching, and merging.
  • Improves Code Readability: Functions reduce repetitive code and make array operations easier to understand.
  • Supports Flexible Data Processing: Callback-based functions allow custom conditions and transformations.
  • Reduces Development Time: Common operations can be performed with concise function calls.
Comment