Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Array Functions
PHP Intermediate FREE

Array Functions

Lesson 2 of 17 Intermediate Interactive

PHP has many powerful built-in array functions that let you transform, search, and manipulate arrays efficiently.

Key Array Functions

  • array_map() — Apply a function to every element
  • array_filter() — Filter elements based on a condition
  • array_reduce() — Reduce array to a single value
  • array_search() — Find a value in an array
  • sort() — Sort an array
  • in_array() — Check if a value exists
  • array_unique() — Remove duplicate values

Tip: These functions do not modify the original array — they return a new one (except sort).

Syntax

PHP
<?php
$numbers = [1, 2, 3, 4, 5];

// array_map — transform each element

$doubled = array_map(fn($n) => $n * 2, $numbers);
// [2, 4, 6, 8, 10]


// array_filter — keep matching elements

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


// array_reduce — single result

$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);
// 15


// array_search

$key = array_search("banana", ["apple", "banana"]);
// 1


// sort

sort($numbers);

// in_array

$exists = in_array(3, $numbers); // true


// array_unique

$duplicates = [1, 1, 2, 3, 3];
$unique = array_unique($duplicates); // [1, 2, 3]

?>
PHP Array Functions
PHP
<?php
$nums = [3, 1, 4, 1, 5];

// Sorting
sort($nums);
echo "Sorted: " . implode(", ", $nums) . "
";

$fruits = ["banana", "apple", "cherry"];
sort($fruits);
echo "Alphabetical: " . implode(", ", $fruits) . "
";

// Map / filter (PHP 7.4+)
$doubled = array_map(fn($n) => $n * 2, $nums);
echo "Doubled: " . implode(", ", $doubled) . "
";

$even = array_filter($nums, fn($n) => $n % 2 === 0);
echo "Even: " . implode(", ", $even) . "
";

// Sum / merge
echo "Sum: " . array_sum($nums) . "
";
$merged = array_merge([1, 2], [3, 4]);
echo "Merged: " . implode(", ", $merged) . "
";
?>

Practice

1
Exercise

Use array_filter() to get only even numbers from $numbers = [1,2,3,4,5,6,7,8,9,10].

Answer
<?php
$numbers = [1,2,3,4,5,6,7,8,9,10];
$evens = array_filter($numbers, fn($n) => $n % 2 === 0);
echo implode(", ", $evens);
?>
2
Exercise

Use array_map() to convert an array of temperatures from Celsius to Fahrenheit.

Answer
<?php
$celsius = [0, 20, 37, 100];
$fahrenheit = array_map(fn($c) => ($c * 9/5) + 32, $celsius);
foreach ($celsius as $i => $c) {
    echo "$c°C = " . $fahrenheit[$i] . "°F<br>";
}
?>

Quick Quiz

1

What does array_filter() do?

array_filter() iterates over each element and keeps only those that return true from the callback.

2

Which function reduces an array to a single value?

array_reduce() applies a callback function cumulatively to reduce the array to a single value.

3

How do you remove duplicate values from an array?

array_unique() removes duplicate values from an array and returns the result.

Interview Questions

array_map() transforms every element by applying a callback function to each one — it always returns an array of the same length. array_filter() selectively keeps or removes elements based on a condition — the resulting array can be shorter. map is for transformation, filter is for selection.

array_reduce() takes an array and a callback function. It starts with an initial value (the third parameter) and iterates through the array, passing the accumulated result and current element to the callback. The callback returns a new accumulated value. This continues until all elements are processed, returning the final single value.