Frank Perez

Frank Perez

Keep in touch

An experienced Web Developer with years of experience in design, programming and server administration.

Blog

Overview

© 2024 by Frank Perez.

PHP's array_reduce is not only for outputting single values

Sometimes we need to partition our data, which can be done in many ways. In this this article I'll show you how to do this using PHP's array_reduce function.

First lets look at parameters that you can pass the function.

  • The first parameter should always be an array.
  • The second parameter is a call back function, which is where all the magic will happen.
  • The third, optional, parameter is where we set the initial value.

First let's look at the basic usage by adding up an array of numbers.

You can also use array_sum, but for the purposes of this tutorial we'll use array_reduce

$numbers = [1, 3, 9, 12, 15, 18, 21];

$result = array_reduce($numbers, function($carry, $number) {
    return $carry + $number;
}, 0);

Our result would be 79 as expected.

Next we'll look at splitting up the numbers array into 2 groups. Small numbers being anything under 10, and large numbers being 10 and over.

$numbers = [1, 3, 9, 12, 15, 18, 21];

$result = [
    'small' => [],
    'large' => [],
];

$result = array_reduce($numbers, function($carry, $number) {
    if($number >= 10) {
        array_push($carry['large'], $number);   
    }

    if($number < 10) {
        array_push($carry['small'], $number);   
    }

    return $carry;
}, $result);

The output of $result would be the following as we expect:

Array
(
    [small] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 9
        )

    [large] => Array
        (
            [0] => 12
            [1] => 15
            [2] => 18
            [3] => 21
        )

)

Lastly, we'll take another example of taking a group of users and splitting them up by gender.

$users = [
    ['name' => 'Frank', 'gender' => 'Male'],
    ['name' => 'Michael', 'gender' => 'Male'],
    ['name' => 'David', 'gender' => 'Male'],
    ['name' => 'Jabari', 'gender' => 'Male'],
    ['name' => 'Sarah', 'gender' => 'Female'],
    ['name' => 'Jessy', 'gender' => 'Female'],
    ['name' => 'Tracey', 'gender' => 'Female'],
];

$usersByGender = array_reduce(
    $users, 
    function($groups, $user) {
        if($user['gender'] === 'Male') {
            array_push($groups['male'], $user);
        }

        if($user['gender'] === 'Female') {
            array_push($groups['female'], $user);
        }

        return $groups;
    }, 
    [
        'male' => [],
        'female' => [],
    ]
);

As expected our output would be the following:

Array
(
    [male] => Array
        (
            [0] => Array
                (
                    [name] => Frank
                    [gender] => Male
                )

            [1] => Array
                (
                    [name] => Michael
                    [gender] => Male
                )

            [2] => Array
                (
                    [name] => David
                    [gender] => Male
                )

            [3] => Array
                (
                    [name] => Jabari
                    [gender] => Male
                )

        )

    [female] => Array
        (
            [0] => Array
                (
                    [name] => Sarah
                    [gender] => Female
                )

            [1] => Array
                (
                    [name] => Jessy
                    [gender] => Female
                )

            [2] => Array
                (
                    [name] => Tracey
                    [gender] => Female
                )

        )

)