Use of PHP array processing functions array_reduce (ii)

Source: Internet
Author: User
Tags php online

More detailed usage of PHP array operation functions You can also refer to the PHP online reference manual: http://php.net/manual/zh/index.php

array_reduce-iteratively to simplify the array to a single value with a callback function

Given an array:

$ar = Array (1,2,3,4,5);

If the sum of each element in this array is required.

Method One:

It's natural to use foreach to implement

1 $sum = 0; 2 foreach ($aras$v) {3     $sum+ =$v; 4 }5echo$sum;
View Code

Method Two:

We can implement it with array_reduce. It is designed to iterate over algebraic groups. The function receives a maximum of three parameters.

The first parameter receives an array of

The second argument function name, or an anonymous function, has two parameters, representing $result and $item, respectively.

The third parameter, optionally, is treated as the first value in the array, or, if the array is empty, as the final return value.

 1  function  Leijia ( $a ,  $b   2   $a  +=$ b  ;  3  return   $a   4   5   $result  = array_reduce  ( $ar , leijia);  6  $ result ; 
View Code

Continue optimization, you can use the anonymous function above PHP5.3. Make the code more streamlined.

1 $result Array_reduce ($ar ,function($a,$b) {2     $a+ =$b ; 3     return $a ; 4 });
View Code

Array_reduce is not only so powerful. Look at the following example. POPs the first element of the array $arr as the initial value, avoiding the $result being empty in min ($result [' min '], $item [' min ']).

Otherwise the final result min is empty.

1 $arr=Array( 2     Array(' min ' = 1.5456, ' max ' = 2.28548, ' volume ' = 23.152),3     Array(' min ' = 1.5457, ' max ' = 2.28549, ' volume ' = 23.152),4     Array(' min ' = 1.5458, ' max ' = 2.28550, ' volume ' = 23.152),5     Array(' min ' = 1.5459, ' max ' = 2.28551, ' volume ' = 23.152),6     Array(' min ' = 1.5460, ' max ' = 2.28552, ' volume ' = 23.152),7 ); 8 9 $initial=Array_shift($arr); Ten  One $t=Array_reduce($arr,function($result,$item) {  A     $result[' min '] =min($result[' min '],$item[' min ']);  -     $result[' max '] =Max($result[' Max '],$item[' Max ']);  -     $result[' volume '] + =$item[' Volume '];  the  -     return $result;  -},$initial);
View Code

In short, this is more elegant than foreach and defines variables less. Recommended use.

obviously , the previous article (the use of PHP array processing functions Array_push (a)) we can implement this:

1 $ids Array_reduce ($userfunction($v,$w) {2         $v[$w ["id"]] =$w["name"]; 3         return $v ; 4     });
View Code

Implementation results:

Use of PHP array processing functions array_reduce (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.