← Back to PHP Course | Chapter 7: Arrays | Lesson 10 of 13

PHP Array Functions

Array functions are ready-made tools for lists, such as counting items, joining lists, or picking out only some. PHP already built them so you do not have to write them yourself.
Syntax
php
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
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `count($fruits)` to the output
echo count($fruits);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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;
?>

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
<?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));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting array_merge to keep numeric keys, when it renumbers them; use + to keep the first array's keys.
  2. Expecting array_filter to reindex the result, when it preserves the original keys.
  3. Forgetting that array_map returns a new array and does not modify the original.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.