PHP Array Functions
In this page:
count($array);
array_merge($array1, $array2);
array_keys($array);
array_values($array);
array_map(fn($x) => expression, $array);
array_filter($array, fn($x) => condition);
Getting Array Length
PHP ships with well over a hundred built-in array functions, covering everything from transforming values to filtering, combining, and inspecting arrays — knowing the common ones saves you from hand-writing loops for routine tasks.
Example: Getting Array Length
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `count($fruits)` to the output
echo count($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Merging Arrays
array_map() applies a given function to every element of an array and returns a new array of the results, which is the idiomatic way to transform a whole list at once, like doubling every number or uppercasing every string.
Example: Merging Arrays
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
$doubled = array_map(function ($n) {
// Return `$n * 2` from this function
return $n * 2;
}, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>
Login to try C/C++/Java/PHP code in the editor
Getting Keys and Values
array_filter() keeps only the elements that satisfy a callback function, returning a new array with the rest removed — useful for tasks like extracting only the active users from a full user list.
Example: Getting Keys and Values
<?php
$users = [
["name" => "Alice", "active" => true],
["name" => "Bob", "active" => false],
];
$activeUsers = array_filter($users, function ($u) {
// Return `$u["active"]` from this function
return $u["active"];
});
// Print a human-readable dump of `$activeUsers`
print_r($activeUsers);
?>
Login to try C/C++/Java/PHP code in the editor
Filtering Arrays
array_reduce() collapses an array down to a single value by repeatedly applying a callback that combines an accumulator with each element, which is how you'd compute something like a running total or a concatenated string from a list.
Example: Filtering Arrays
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
$total = array_reduce($numbers, function ($carry, $n) {
// Return `$carry + $n` from this function
return $carry + $n;
}, 0);
// Print `$total` to the output
echo $total;
?>
Login to try C/C++/Java/PHP code in the editor
Mapping Array Elements
in_array() and array_key_exists() check for a value or a key respectively, and it's easy to confuse them — in_array() searches the values, array_key_exists() searches the keys, and mixing them up is a common source of bugs.
Example: Mapping Array Elements
<?php
// Declare `$fruits` as an array: `["apple", "banana"]`
$fruits = ["apple", "banana"];
// Print a detailed dump (with types) of `in_array("apple", $fruits)`
var_dump(in_array("apple", $fruits));
// Declare `$person` as an array: `["name" => "Alice"]`
$person = ["name" => "Alice"];
// Print a detailed dump (with types) of `array_key_exists("name", $person)`
var_dump(array_key_exists("name", $person));
?>
Login to try C/C++/Java/PHP code in the editor
- Expecting
array_mergeto keep numeric keys, when it renumbers them; use+to keep the first array's keys. - Expecting
array_filterto reindex the result, when it preserves the original keys. - Forgetting that
array_mapreturns a new array and does not modify the original.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: