php-funciones-para-trabajar-con-arrays-en-php

Functions for Working with Arrays in PHP

  • 3 min

Array functions allow you to transform, filter, and combine collections of data without repeating the same logic manually.

Until now, if you wanted to modify a list of numbers, you probably made a foreach loop, created an empty variable outside, and filled it in.

That is “Imperative” programming (you tell the computer how to do things). Today we are going to look at “Declarative” programming (you tell the computer what you want to achieve).

We have three main tools that cover 90% of use cases with arrays.

array_map: the transformer

Mission: You have a list of things and you want to transform each one of them into something else, keeping the same number of elements.

Example: You have prices without VAT and you want to calculate the total for each one.

$prices = [10, 20, 50];

// Modern style with Arrow Functions (fn)
$pricesWithVAT = array_map(fn($p) => $p * 1.21, $prices);

// Result: [12.1, 24.2, 60.5]
Copied!

Notice the cleanliness. No loops, no temporary variables. One line.

Watch the order: In array_map, the function comes first, then the array.

array_filter: the filter

Mission: You have a list and you want to keep only those that meet a condition.

Example: You have a list of users and you want only those of legal age.

$ages = [15, 18, 25, 12, 40];

$adults = array_filter($ages, fn($age) => $age >= 18);

// Values preserved: [1 => 18, 2 => 25, 4 => 40]
Copied!

Beware of the indexes! array_filter preserves the original indexes. The result above could be [1 => 18, 2 => 25, 4 => 40]. If you want to re-index the array (so it goes back to 0, 1, 2…), wrap it in array_values().

array_reduce: the accumulator

Mission: You have a list of things and you want to reduce them to a single final value.

Example: Sum the total of a shopping cart.

It works with a variable called the “accumulator” (or carry) that carries the result round after round.

$cart = [10, 20, 30];

// (Accumulator, Current Item)
$total = array_reduce($cart, fn($acc, $item) => $acc + $item, 0);

// Result: 60
Copied!

The 0 at the end is the initial value of the accumulator.

Watch the order: Unlike map, in filter and reduce the array comes first, then the function. Yes, PHP is famous for this inconsistency 🙅

Array Destructuring

Destructuring is useful when you have an array with coordinates [x, y] and you want to pass them to separate variables $x and $y.

The old (ugly) way:

$point = [10, 20];
$x = $point[0];
$y = $point[1];
Copied!

The modern way (Destructuring): We use the brackets [] to the left of the equals sign to “unpack” the array.

$point = [10, 20];

[$x, $y] = $point; // Done!
// Now $x is 10, $y is 20
Copied!

Destructuring Associative Arrays

It also works with keys, which is incredibly useful for working with APIs or configurations.

$book = [
    'title' => 'Don Quixote',
    'author'  => 'Cervantes',
    'year'   => 1605
];

// I want to extract only the title and year into variables
['title' => $myTitle, 'year' => $myYear] = $book;

echo $myTitle; // "Don Quixote"
Copied!

The “Swap” Trick

A classic interview question: “How do you swap the value of two variables without using a third temporary variable?” In modern PHP it’s trivial thanks to destructuring:

$a = 1;
$b = 2;

[$b, $a] = [$a, $b];
// Now $a is 2 and $b is 1
Copied!