PHP array_reduce Function | Reduce Array To Single Value

PHP array_reduce is an inbuilt function in PHP. This function reduces an array to a single value which can be an integer, float or string. It accepts a user-defined callback function. In this article, we will discuss the PHP array_reduce Function. Also, we will discuss a few examples of its usage.

Syntax

array_reduce($array, callback_function, $initial = null)

Parameters

The PHP array_reduce Function has three parameters. Two parameters are mandatory while the third is optional.

  • $array: The First parameter of PHP array_reduce Function is the array. It refers to the array which we want to reduce. Also, it is a mandatory parameter.
  • callback_function: Secondly, we need to pass the callback function which we want to use to reduce the array.
  • $initial: The third parameter is the initial value sent to the function. Moreover, it is an optional parameter with default value as null.

Return Value

The PHP array_reduce Function returns a single value. It can be of integer, float or string type.

The Callback Function

The callback function supplied to the array_reduce function in PHP is of the following format:

<?php
	function function_name ($accumulator, $value) {

	}
?>

The function should contain two parameters.

  • $accumulator: The first parameter is an accumulator which stores the result-in-progress effectively assembled. It starts out with null value unless the $initial is passed.
  • $value: The second parameter is the current value which is passed during each step of reduction.

The return value of the callback becomes the new value of the accumulator.

Examples

Let’s discuss a few examples demonstrating the use of PHP array_reduce Function.

Example 1: Sum Integers in Array

For instance, let’s consider an example to sum integers in an array.

<?php
	function arraySum ($accumulator, $value) {
		return $accumulator + $value;
	}

	$testArray = array(1,3,5,7,9);
	print_r(array_reduce($testArray, 'arraySum'));
	/*
	OUTPUT: 
	25
	*/
?>

The above code snippet demonstrates the use PHP array_reduce Function to sum elements in an array. The array is reduced to a single value according to ‘+’ operator.

Example 2: Passing an Initial Value

For example, if we pass an initial value then the result will be affected as follows:

<?php
	function arraySum ($accumulator, $value) {
		return $accumulator + $value;
	}

	$testArray = array(1,3,5,7,9);
	print_r(array_reduce($testArray, 'arraySum', 10));
	/*
	OUTPUT: 
	35
	*/
?>

In the code same as above, we make a minor change. On passing the $initial parameter as 10, the total sum now becomes 35 instead of 25.

Example 3: Operating on Strings

For instance, let’s implement PHP array_implode Function using array_reduce.

<?php
	function arrayImplode ($accumulator, $value) {
		return $accumulator.'-'.$value;
	}

	$testArray = array('Hi', 'How', 'Are', 'You?');
	print_r(array_reduce($testArray, 'arrayImplode'));
	/*
	OUTPUT: 
	-Hi-How-Are-You?
	*/
?>

The above code joins the strings with hyphen. Also, notice the initial hyphen in the final string. This is because we have not passed the initial value which is taken as null by default.

Conclusion

In conclusion, we discussed array_reduce Function in PHP. It can be used to reduce an array to a single value using a user-defined callback function. Moreover, you can go through other PHP array Functions on Concatly.

Also, for more information on array_reduce function , go through PHP Official Documentation.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *