PHP array_reduce Function (original) (raw)

Summary: in this tutorial, you will learn how to use the PHP array_reduce() function to reduce an array to a single value.

Introduction to the PHP array_reduce function #

The array_reduce() function reduces an array to a single value using a callback function. It’s easier to understand the array_reduce() function by example.

The following example calculates the sum of all numbers in an array:

`<?php

$numbers = [10,20,30];

$total = 0;

foreach ($numbers as $number) { total+=total += total+=number; }

echo $total; // 60`Code language: PHP (php)

Try it

How it works.

Alternatively, you can use the array_reduce() function to achieve the same result without using the foreach statement:

`<?php

$numbers = [10,20,30]; total=arrayreduce(total = array_reduce(total=arrayreduce(numbers, function ($previous, $current) { return previous+previous + previous+current; });

echo $total; // 60`Code language: PHP (php)

Try it

The array_reduce() function accepts an array and a callback function. It reduces the $numbers array to a single value using the callback function.

Since PHP 7.3, you can use an arrow function rather than an anonymous function as the callback function like this:

`<?php

$numbers = [10,20,30];

$total = array_reduce( $numbers, fn ($previous, current)=>current) => current)=>previous + $current );

echo $total; // 60`Code language: PHP (php)

Try it

The following shows the array_reduce() function’s syntax:

array_reduce ( array <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo separator="true">,</mo><mi>c</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">array , callable </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">ab</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span></span></span></span>callback , mixed $initial = null ) : mixedCode language: PHP (php)

The array_reduce() function has the following parameters:

If the input array is empty or the $initial is omitted, the array_reduce() function returns null.

The $callback function is often called a reducer. It’s where you decide the logic for reducing the array elements. The callback function has the following signature:

callback ( mixed <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>a</mi><mi>r</mi><mi>r</mi><mi>y</mi><mo separator="true">,</mo><mi>m</mi><mi>i</mi><mi>x</mi><mi>e</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">carry , mixed </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">rry</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">mi</span><span class="mord mathnormal">x</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span></span></span></span>item ) : mixedCode language: PHP (php)

The callback function accepts two arguments:

PHP array_reduce function example #

The following example uses the array_reduce() function to calculate the total items in a shopping cart:

`<?php

$carts = [ ['item' => 'A', 'qty' => 2, 'price' => 10], ['item' => 'B', 'qty' => 3, 'price' => 20], ['item' => 'C', 'qty' => 5, 'price' => 30] ];

$total = array_reduce( $carts, function ($prev, $item) { return prev+prev + prev+item['qty'] * $item['price']; } );

echo $total; `Code language: PHP (php)

Try it

Output:

230

If the carts array is empty, you’ll get the total as null.

To return zero instead of null, you pass the initial argument as zero to the array_reduce() function like this:

`<?php

$carts = [];

$total = array_reduce( $carts, function ($prev, $item) { return prev+prev + prev+item['qty'] * $item['price']; }, 0 );

echo $total; // 155`Code language: PHP (php)

Try it

Output:

0Code language: PHP (php)

Since the $carts array is empty, the total is zero.

Summary #

Did you find this tutorial useful?