0
0
PHPprogramming~5 mins

Closures in array functions in PHP

Choose your learning style9 modes available
Introduction

Closures let you write small, quick functions inside array functions to change or check each item easily.

When you want to change every item in a list in a simple way.
When you want to pick only some items from a list based on a rule.
When you want to add up or combine all items in a list.
When you want to check if any or all items in a list meet a condition.
Syntax
PHP
<?php
// Example of using a closure with array_map
$array = [1, 2, 3];
$newArray = array_map(function($item) {
    return $item * 2;
}, $array);
?>

A closure is an anonymous function you write right where you use it.

Array functions like array_map, array_filter, and array_reduce accept closures to work on each item.

Examples
This doubles every number in the list.
PHP
<?php
// Double each number in the array
$numbers = [1, 2, 3];
$doubled = array_map(function($number) {
    return $number * 2;
}, $numbers);
print_r($doubled);
?>
This keeps only numbers 3 or bigger.
PHP
<?php
// Filter out numbers less than 3
$numbers = [1, 2, 3, 4];
$filtered = array_filter($numbers, function($number) {
    return $number >= 3;
});
print_r($filtered);
?>
This adds all numbers together.
PHP
<?php
// Sum all numbers
$numbers = [1, 2, 3];
$sum = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
}, 0);
echo $sum;
?>
This finds if there is any even number in the list.
PHP
<?php
// Check if any number is even
$numbers = [1, 3, 5, 6];
$hasEven = array_filter($numbers, function($number) {
    return $number % 2 === 0;
});
if (!empty($hasEven)) {
    echo "There is an even number.";
} else {
    echo "No even numbers.";
}
?>
Sample Program

This program shows how closures work with array functions to change, filter, and add numbers in a list.

PHP
<?php
// Create an array of numbers
$numbers = [1, 2, 3, 4, 5];

// Print original array
echo "Original array:\n";
print_r($numbers);

// Use closure with array_map to square each number
$squaredNumbers = array_map(function($number) {
    return $number * $number;
}, $numbers);

echo "\nSquared numbers:\n";
print_r($squaredNumbers);

// Use closure with array_filter to keep only numbers greater than 10
$filteredNumbers = array_filter($squaredNumbers, function($number) {
    return $number > 10;
});

echo "\nFiltered numbers (greater than 10):\n";
print_r($filteredNumbers);

// Use closure with array_reduce to sum all filtered numbers
$sum = array_reduce($filteredNumbers, function($carry, $item) {
    return $carry + $item;
}, 0);

echo "\nSum of filtered numbers: $sum\n";
?>
OutputSuccess
Important Notes

Time complexity depends on the array size and the function inside the closure.

Closures use little extra memory but keep your code clean and easy to read.

Common mistake: forgetting to return a value inside the closure.

Use closures in array functions when you want quick, small operations on each item without writing separate named functions.

Summary

Closures are small, unnamed functions used inside array functions.

They help you change, filter, or combine array items easily.

Using closures keeps your code short and clear.