English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

Object function

PHP Array Function Manual

PHP array_reduce() function usage and example

The PHP array_reduce() function uses a callback function to iteratively reduce an array to a single value

Syntax

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

Definition and usage

The array_reduce() function iteratively applies the callback function callback to each element of the array array, thereby reducing the array to a single value.

ParameterNumber
1

Parameters and descriptions

array (required)

2

It specifies an array.

function (required)

3

This is a callback function.

initial (optional)

It specifies the initial value to be sent to the function.

Return value

It returns the array result value.

Online Example

Example
   <?php1function call_back_function($v2, $v
      ) {1 return $v-. ";2" . $v
   ;
   }
   
   $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));
?>
Test and see‹/›

Output result:

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

 PHP Array Function Manual