Array Functions
PHP provides many built-in functions for array manipulation. This page serves as a quick reference organized by use case.
For basic array operations (creating, accessing, adding, removing elements), see PHP Arrays.
PHP Array Functions - Full Documentation ↗
Array Information
Section titled “Array Information”count()
Section titled “count()”Returns the number of elements.
$fruits = ['apple', 'banana', 'orange'];echo count($fruits); // 3is_array()
Section titled “is_array()”Checks if a variable is an array.
is_array(['a', 'b']); // trueis_array('hello'); // falsearray_key_exists()
Section titled “array_key_exists()”Checks if a key exists (works even when the value is null).
$user = ['name' => 'John', 'email' => null];
array_key_exists('email', $user); // trueisset($user['email']); // false (null treated as not set)in_array()
Section titled “in_array()”Checks if a value exists.
$fruits = ['apple', 'banana', 'orange'];
in_array('banana', $fruits); // truein_array('4', [1, 2, 3, 4]); // true (loose comparison)in_array('4', [1, 2, 3, 4], true); // false (strict comparison)Keys and Values
Section titled “Keys and Values”array_keys()
Section titled “array_keys()”Returns all keys.
$person = ['name' => 'John', 'age' => 30];array_keys($person); // ['name', 'age']array_values()
Section titled “array_values()”Returns all values with numeric keys.
$person = ['name' => 'John', 'age' => 30];array_values($person); // ['John', 30]array_flip()
Section titled “array_flip()”Swaps keys and values.
$original = ['a' => 1, 'b' => 2];array_flip($original); // [1 => 'a', 2 => 'b']Searching and Filtering
Section titled “Searching and Filtering”array_search()
Section titled “array_search()”Finds the key for a given value.
$fruits = ['apple', 'banana', 'orange'];array_search('banana', $fruits); // 1array_search('grape', $fruits); // falsearray_filter()
Section titled “array_filter()”Filters elements using a callback.
$numbers = [1, 2, 3, 4, 5, 6];
$evens = array_filter($numbers, fn($n) => $n % 2 === 0);// [2, 4, 6]array_column()
Section titled “array_column()”Extracts a column from a multidimensional array.
$users = [ ['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane']];
array_column($users, 'name'); // ['John', 'Jane']array_column($users, 'name', 'id'); // [1 => 'John', 2 => 'Jane']array_unique()
Section titled “array_unique()”Removes duplicate values.
$colors = ['red', 'blue', 'red', 'green'];array_unique($colors); // ['red', 'blue', 'green']array_intersect()
Section titled “array_intersect()”Returns values present in all arrays.
$a = ['a', 'b', 'c'];$b = ['b', 'c', 'd'];array_intersect($a, $b); // ['b', 'c']array_diff()
Section titled “array_diff()”Returns values from the first array not present in others.
$a = ['a', 'b', 'c', 'd'];$b = ['b', 'c'];array_diff($a, $b); // ['a', 'd']Transforming Arrays
Section titled “Transforming Arrays”array_map()
Section titled “array_map()”Applies a callback to each element.
$numbers = [1, 2, 3, 4];$squared = array_map(fn($n) => $n * $n, $numbers);// [1, 4, 9, 16]array_walk()
Section titled “array_walk()”Applies a callback that can modify elements in place.
$prices = ['apple' => 1.50, 'banana' => 0.75];
array_walk($prices, function(&$price) { $price = round($price * 1.08, 2); // Add 8% tax});// ['apple' => 1.62, 'banana' => 0.81]array_reduce()
Section titled “array_reduce()”Reduces an array to a single value.
$numbers = [1, 2, 3, 4, 5];$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);// 15array_chunk()
Section titled “array_chunk()”Splits an array into chunks.
$data = [1, 2, 3, 4, 5, 6];array_chunk($data, 2); // [[1, 2], [3, 4], [5, 6]]array_slice()
Section titled “array_slice()”Extracts a portion of an array.
$fruits = ['apple', 'banana', 'orange', 'grape'];array_slice($fruits, 1, 2); // ['banana', 'orange']array_slice($fruits, -2); // ['orange', 'grape']array_reverse()
Section titled “array_reverse()”Reverses element order.
$numbers = [1, 2, 3];array_reverse($numbers); // [3, 2, 1]implode() and explode()
Section titled “implode() and explode()”Convert between arrays and strings.
$fruits = ['apple', 'banana', 'orange'];implode(', ', $fruits); // "apple, banana, orange"
$text = "one,two,three";explode(',', $text); // ['one', 'two', 'three']Combining Arrays
Section titled “Combining Arrays”array_merge()
Section titled “array_merge()”Merges arrays together.
$a = ['a', 'b'];$b = ['c', 'd'];array_merge($a, $b); // ['a', 'b', 'c', 'd']
// With associative arrays, later values overwrite earlier ones$defaults = ['color' => 'blue', 'size' => 'M'];$custom = ['color' => 'red'];array_merge($defaults, $custom); // ['color' => 'red', 'size' => 'M']array_combine()
Section titled “array_combine()”Creates an array using one array for keys and another for values.
$keys = ['name', 'email'];$values = ['John', 'john@example.com'];array_combine($keys, $values); // ['name' => 'John', 'email' => 'john@example.com']array_replace()
Section titled “array_replace()”Replaces values by key.
$base = ['a' => 1, 'b' => 2];$new = ['b' => 20, 'c' => 3];array_replace($base, $new); // ['a' => 1, 'b' => 20, 'c' => 3]Sorting
Section titled “Sorting”All sorting functions modify the array in place.
By Value
Section titled “By Value”| Function | Order | Maintains Keys |
|---|---|---|
sort() | Ascending | No |
rsort() | Descending | No |
asort() | Ascending | Yes |
arsort() | Descending | Yes |
$fruits = ['orange', 'apple', 'banana'];sort($fruits); // ['apple', 'banana', 'orange']
$ages = ['John' => 25, 'Jane' => 30, 'Bob' => 20];asort($ages); // ['Bob' => 20, 'John' => 25, 'Jane' => 30]By Key
Section titled “By Key”| Function | Order |
|---|---|
ksort() | Ascending |
krsort() | Descending |
$data = ['c' => 3, 'a' => 1, 'b' => 2];ksort($data); // ['a' => 1, 'b' => 2, 'c' => 3]Custom Sorting
Section titled “Custom Sorting”Use usort() for custom comparison logic. The spaceship operator (<=>) returns -1, 0, or 1.
$people = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30], ['name' => 'Bob', 'age' => 20]];
usort($people, fn($a, $b) => $a['age'] <=> $b['age']);// Sorted by age: Bob (20), John (25), Jane (30)Use uasort() to preserve keys, or uksort() to sort by keys with custom logic.
Sorting Flags
Section titled “Sorting Flags”$data = ['10', '2', '1', '20'];
sort($data); // ['1', '10', '2', '20'] (string comparison)sort($data, SORT_NUMERIC); // ['1', '2', '10', '20'] (numeric comparison)