How to use array reduce in PHP

In this article, I will explain how the array_reduce() function can be used in PHP.
  • 1930

array_reduce() function in PHP

  • The array_reduce() function is used to sends the values in an array to a user-defined function, and returns a string.

  • In array_reduce() function, If the optional initial is available, it will be used at the beginning of the process.

Syntax

array_reduce(array,function,initial)

Parameter

  • array  array is required parameter. Specifies an array.

  • function number is required parameter.  User define function for input the value in array.

  • initial initial is optional parameter. determine the initial value to send to the function

Example

The following example show to how the array_reduce() function can be used in PHP.

<html>

<body>

<h3 style="color: red;">array_reduce() function example in PHP</h3>  

    <?php 

    function add($a1, $a2) 

    { 

    $a1 += $a2; 

    return $a1; 

    } 

    $array1 = array(1, 2,3,4,5); 

    $x=array_reduce($array1,"add"); 

    echo " value is : ".$x; 

    ?>

</body>

</html>

 

Output

reduce.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.