PHP - Function array_reduce()


Advertisements


Syntax

array_reduce ( $array, callback $function [, int $initial] );

Definition and Usage

This function applies iteratively the function function to the elements of the array, so as to reduce the array to a single value.

Parameters

Sr.No Parameter & Description
1

array(Required)

It specifies an array.

2

function(Required)

This a callback function.

3

initial(Optional)

It specifies the initial value to send to the function.

Return Values

It returns a reduced array.

Example

Try out following example −

<?php
   function call_back_function($v1,$v2) {
      return $v1 . "-" . $v2;
   }
	
   $input = array("a"=>"banana","b"=>"apple","c"=>"orange");
   
   print_r(array_reduce($input, call_back_function));
   print_r("<br />");
   print_r(array_reduce($input, call_back_function, 10));
?> 

This will produce the following result −

-banana-apple-orange
10-banana-apple-orange

php_function_reference.htm

Advertisements