Manipulating Data with Ease: Understanding the Power of Map, Filter, and Fold in PHP

Laravel_learn
4 min readFeb 5, 2023

The Map, Filter, and Fold Functions: Understanding Their Significance in PHP As a programming language, PHP is widely used for web development, and lists are a universal aspect of programming. They are important for storing multiple values, with the basic list type in PHP being the associative array, also known as a hash table.

Besides this type, there are other list types available in PHP, such as the SPL fixed array, object storage, doubly linked lists, heaps, and stacks. Additionally, there exist immutable lists and collections which form another grouping. Regardless of the list type, manipulating data stored in lists is an important requirement in programming. While it is possible to manipulate list data using iterative control structures such as for, while, foreach loops and the like, the map, filter, and fold functions provide a more stylistically appropriate and efficient way to modify lists.

These functions are referred to as higher-order functions, and they create new list data by applying functions in one go, with each function differing from the others algorithmically.

The Map Function

The map function is used to apply a function to each value in a list. The PHP userland has an array_map function, which is convenient for this purpose, and multiple arrays can be parsed by the array_map function.

The following example demonstrates the mapping of the factorial function on each value in a list of numbers using the array_map function:

<?php

define(‘NUMBERS’, [1, 2, 3, 4, 5]);

function factorial(int $val): int {

return $val < 2 ? 1 : factorial($val-1) * $val;

}

$result = array_map(‘factorial’, NUMBERS); }

The output of the array_map operation ($result) is an array of factorials. The same operation can also be performed using the map function from the bingo-functional library. The code would look like this:

<?php require __DIR__ . ‘/vendor/autoload.php’;

use function Chemem\Bingo\Functional\map; define(‘NUMBERS’, [1, 2, 3, 4, 5]);

function factorial(int $val): int { return $val < 2 ? 1 : factorial($val — 1) * $val;

}

In this case, the performance of both the array_map and map functions are similar for the factorial function mapping case. However, the map function can’t allow for multiple array function mappings, unlike the array_map function, which can include another list in the array_map sequence and generate a more comprehensive two-list result. Fusion with array_merge() and the Spread Operator The following example maps the same factorial function onto each value in two different lists (FACT_KEYS and NUMBERS):

<?php require __DIR__ . ‘/functions.php’;

define(‘NUMBERS’, [1, 2, 3, 4, 5]);

define(‘FACT_KEYS’, [‘1!’, ‘2!’, ‘3!’, ‘4!’, ‘5!’]);

$result = array_merge( …array_map( fn (string $key, int $value): array => [$key => factorial($value)],

FACT_KEYS, The PHP spread operator (…) is used in the array”

The Filter Function

The filter function is used to extract values from a list based on a specified condition. The PHP userland has an array_filter function, which is convenient for this purpose. The following example demonstrates the filtering of numbers greater than 3 in a list of numbers using the array_filter function:

<?php

define(‘NUMBERS’, [1, 2, 3, 4, 5]);

$result = array_filter(NUMBERS, fn($val): bool => $val > 3);

The output of the array_filter operation ($result) is an array of numbers greater than 3. The same operation can also be performed using the filter function from the bingo-functional library. The code would look like this:

require __DIR__ . ‘/vendor/autoload.php’;

use function Chemem\Bingo\Functional\filter; define(‘NUMBERS’, [1, 2, 3, 4, 5]);

$result = filter(fn($val): bool => $val > 3, NUMBERS);

The Fold Function The fold function is used to reduce a list to a single value by applying a function to the elements of a list in a sequential manner. The PHP userland has an array_reduce function, which is convenient for this purpose. The following example demonstrates the calculation of the sum of all elements in a list of numbers using the array_reduce function:

define(‘NUMBERS’, [1, 2, 3, 4, 5]);

$result = array_reduce(NUMBERS, fn($carry, $val): int => $carry + $val, 0);

The output of the array_reduce operation ($result) is the sum of all elements in the list. The same operation can also be performed using the fold function from the bingo-functional library. The code would look like this:

require __DIR__ . ‘/vendor/autoload.php’;

use function Chemem\Bingo\Functional\fold; define(‘NUMBERS’, [1, 2, 3, 4, 5]);

$result = fold(fn($carry, $val): int => $carry + $val, 0, NUMBERS);

The map, filter, and fold functions provide an efficient and elegant way to manipulate lists in PHP. They allow for the application of functions to lists in a single go, providing an alternative to the iterative control structures commonly used for list manipulation. The use of these functions leads to a more concise and maintainable codebase, with improved performance compared to the equivalent iterative control structures. Whether using the standard PHP userland or the functional libraries, these functions should be a staple in the toolkit of every PHP developer.

--

--