PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




array_reduce() Function in PHP


Tutorials »Php »

Topic

What is array_reduce() Function?



Explanation

The "array_reduce()" function is used to send the values in an array to a user defined function and return a string.

Syntax:
    array_reduce(array,function,initial value)

In the above syntax "array" specifies the array from which the values are passed to the function, even one can specify the initial value to be passed.

Example
    <?php
    function funct1($x1,$x2) 
      {
       return $x1 . "-" . $x2;
      }
    function funct2 ($y1,$y2)
      {
       return $y1+$y2
      }
    $a=array("Orange","Guava","Apple");
    $b=array(5,10,20); 
    print_r(array_reduce($a,"funct1",6));
    print "<br>";
    print_r(array_reduce($b,"funct2",6));
    ?>
Result:
    6-Orange-Guava-Apple
    41 

In the above example "funct1" returns a string with the intial value 6 added to the existing array, in the second "funct2" it returns the sum of the numbers of the array "$b".





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting