php

PHP array_reduce() Function

PHP has many built-in functions to work with the array variables. The array_reduce() function is one of the built-in functions of PHP to reduce the array values into a single value by using a callback function. This function is useful when generating content by combining the array of values based on the requirements. The syntax of this function is given below.

Syntax:

mixed array_reduce(array $array, callable $callback [, mixed $initial = null])

The first argument is mandatory that takes an array, and the output will be returned based on this array’s values.

The second argument is mandatory, which defines the name of the callback function, and this function will be used to generate the single output by using array values.

The third argument is optional, and it is used to set the initial values to work with array values. The default value of this argument is null.

The function will return a single output that can be a string, integer, or float. If the array is empty, it will return the initial value used in the third argument.

array_reduce() Function Examples

The uses of the array_reduce() function have been shown in the next part of this tutorial by using multiple examples.

Example-1: Use of array_reduce() function without initial value

Create a PHP file with the following script to generate a string by combining the array values with the space. The callback function named callback_function() has been defined to use inside the array_reduce() function do the task. An array of 4 string values has been declared in the script. The array_reduce() function has been used here without any initial value. So, the default value, null, will be used to generate the output of the array_reduce() function.

<?php

//Declare the callback function

function callback_function($v1, $v2)

{

//Combine the values with space

$v .= $v1." ".$v2;

//Return the combine value

return $v;

}

//Declare the array of string data

$array = array('I', 'like', 'php', 'programming');

echo "<b>The output of the array_reduce() function without initial value:</b><br/>";

//Print the returned value of the array_reduce() function

echo array_reduce($array, "callback_function");

?>

Output:

The following output will be appeared after executing the above script.

Example-2: Use of array_reduce() Function With Initial Value

Create a PHP file with the following script to generate a string by combining the array values with the comma(,). The callback function named callback_function() has been defined to use inside the array_reduce() function do the task. An array of 5 string values has been declared in the script. The array_reduce() function has been used here with the initial value, ‘php’, which will generate the output of the array_reduce() function.

<?php

//Declare the callback function

function callback_function($value1, $value2)

{

return $value1 . " , " . $value2;

}

//Declare the array of string data

$array = array('python', 'perl', 'bash', 'java','C++');

echo "<b>The output of the array_reduce() function with initial value:</b><br/>";

echo array_reduce($array, "callback_function", 'php');

?>

Output:

The following output will be appeared after executing the above script.

Example-3: Calculate the Sum of All Positive Numbers of the Array

Create a PHP file with the following script to calculate the sum of all positive numbers of the array. The callback function named callback_function() has been defined to use inside the array_reduce() function do the task. An array of 7 numbers has been declared in the script that contains 4 positive numbers. The array_reduce() function has been used here with the initial value, 0, and this value will be used to generate the output of the array_reduce() function.

<?php

//Declare the callback function

function callback_function($v1, $v2)

{

if($v2 > 0)

$v1 += $v2;

return $v1;

}

//Declare the array of positive and negative numbers

$array = array(9, -5, 3, -7, 1, 7, -4);

echo "<b>The array values are: </b><br/>";

//Print the array values

print_r($array);

echo "<br/><b>The sum of all positive numbers is:</b><br/>";

//Print the sum of all positive numbers of the array

echo array_reduce($array, "callback_function", 0);

?>

Output:

The following output will be appeared after executing the above script. The sum of 9, 3, 1, and 7 is 20, shown in the output.

Example-4: Multiply the Array Values

Create a PHP file with the following script that will calculate the multiplication result of the numerical values of the array. No callback function has been declared separately in the script, and the function of a single statement has been used inside the array_reduce() function to do the multiplication. An array of 4 numbers has been declared in the script. The array_reduce() function has been used here with the initial value, 1, and this value will be used to generate the output of the array_reduce() function.

<?php

//Declare the array of numbers

$array = array(4, 5, 3, 5);

//Multiply the array values

$multiply = array_reduce($array, fn ($v1, $v2) => $v1 * $v2, 1);

echo "<b>The array values are: </b><br/>";

//Print the array values

print_r($array);

echo "<br/><b>The multiplication value of all elements of the array is:</b><br/>";

echo $multiply;

?>

Output:

The following output will be appeared after executing the above script. The multiplication result of 4, 5, 3, and 5 is 300, shown in the output.

Example-5: Use of array_reduce() in Two-Dimensional Array

Create a PHP file with the following script to calculate the sum of three numeric values of a two-dimensional array using the array_reduce() function. No callback function has been declared separately in the script like in the previous example, and the function with multiple statements has been used inside the array_reduce() function to do the summation.

<?php

//Declare a two-dimensional array

$students = [

['name' => 'Rakib Hossain', 'Physics' => 90, 'Chemistry' => 75, 'Math' => 95],

['name' => 'Neela Akter', 'Physics' => 67, 'Chemistry' => 81, 'Math' => 78],

['name' => 'Kabir Hasan', 'Physics' => 71, 'Chemistry' => 55, 'Math' => 74],

['name' => 'Mizanur Rahman', 'Physics' => 70, 'Chemistry' => 82, 'Math' => 89]

];

//Print the total marks of each student using array_reduce() function

$total = array_reduce(

$students,

function ($init=0, $val) {

//Calculate the sum of all subjects

$sum = $val['Physics'] + $val['Chemistry'] + $val['Math'];

//Print the marks with the name

echo $val['name']." has got ".$sum." marks. <br/>";

return 1;

}

);

?>

Output:

The following output will be appeared after executing the above script.

Conclusion

Different ways of using the array_reduce() function have been shown in the examples of this tutorial to help PHP users to know the use of this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.