Skip to content

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


Returns the number of elements.

$fruits = ['apple', 'banana', 'orange'];
echo count($fruits); // 3

Checks if a variable is an array.

is_array(['a', 'b']); // true
is_array('hello'); // false

Checks if a key exists (works even when the value is null).

$user = ['name' => 'John', 'email' => null];
array_key_exists('email', $user); // true
isset($user['email']); // false (null treated as not set)

Checks if a value exists.

$fruits = ['apple', 'banana', 'orange'];
in_array('banana', $fruits); // true
in_array('4', [1, 2, 3, 4]); // true (loose comparison)
in_array('4', [1, 2, 3, 4], true); // false (strict comparison)

Returns all keys.

$person = ['name' => 'John', 'age' => 30];
array_keys($person); // ['name', 'age']

Returns all values with numeric keys.

$person = ['name' => 'John', 'age' => 30];
array_values($person); // ['John', 30]

Swaps keys and values.

$original = ['a' => 1, 'b' => 2];
array_flip($original); // [1 => 'a', 2 => 'b']

Finds the key for a given value.

$fruits = ['apple', 'banana', 'orange'];
array_search('banana', $fruits); // 1
array_search('grape', $fruits); // false

Filters elements using a callback.

$numbers = [1, 2, 3, 4, 5, 6];
$evens = array_filter($numbers, fn($n) => $n % 2 === 0);
// [2, 4, 6]

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']

Removes duplicate values.

$colors = ['red', 'blue', 'red', 'green'];
array_unique($colors); // ['red', 'blue', 'green']

Returns values present in all arrays.

$a = ['a', 'b', 'c'];
$b = ['b', 'c', 'd'];
array_intersect($a, $b); // ['b', 'c']

Returns values from the first array not present in others.

$a = ['a', 'b', 'c', 'd'];
$b = ['b', 'c'];
array_diff($a, $b); // ['a', 'd']

Applies a callback to each element.

$numbers = [1, 2, 3, 4];
$squared = array_map(fn($n) => $n * $n, $numbers);
// [1, 4, 9, 16]

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]

Reduces an array to a single value.

$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);
// 15

Splits an array into chunks.

$data = [1, 2, 3, 4, 5, 6];
array_chunk($data, 2); // [[1, 2], [3, 4], [5, 6]]

Extracts a portion of an array.

$fruits = ['apple', 'banana', 'orange', 'grape'];
array_slice($fruits, 1, 2); // ['banana', 'orange']
array_slice($fruits, -2); // ['orange', 'grape']

Reverses element order.

$numbers = [1, 2, 3];
array_reverse($numbers); // [3, 2, 1]

Convert between arrays and strings.

$fruits = ['apple', 'banana', 'orange'];
implode(', ', $fruits); // "apple, banana, orange"
$text = "one,two,three";
explode(',', $text); // ['one', 'two', 'three']

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']

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']

Replaces values by key.

$base = ['a' => 1, 'b' => 2];
$new = ['b' => 20, 'c' => 3];
array_replace($base, $new); // ['a' => 1, 'b' => 20, 'c' => 3]

All sorting functions modify the array in place.

FunctionOrderMaintains Keys
sort()AscendingNo
rsort()DescendingNo
asort()AscendingYes
arsort()DescendingYes
$fruits = ['orange', 'apple', 'banana'];
sort($fruits); // ['apple', 'banana', 'orange']
$ages = ['John' => 25, 'Jane' => 30, 'Bob' => 20];
asort($ages); // ['Bob' => 20, 'John' => 25, 'Jane' => 30]
FunctionOrder
ksort()Ascending
krsort()Descending
$data = ['c' => 3, 'a' => 1, 'b' => 2];
ksort($data); // ['a' => 1, 'b' => 2, 'c' => 3]

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.

$data = ['10', '2', '1', '20'];
sort($data); // ['1', '10', '2', '20'] (string comparison)
sort($data, SORT_NUMERIC); // ['1', '2', '10', '20'] (numeric comparison)