Array_map,array_filter,array_walk Differences in PHP

Source: Internet
Author: User


Array_walk:

Array_walk () returns a Boolean value that is specified as a reference to the first parameter of the callback function if the callback function needs to act directly on the value in the array. Any changes to these units will also change the original array itself. This function can get both the key and value of the array at the same time.

Array_map:

Array_map () returns an array that contains the cells of all cells in the arr1 after the callback function. The number of arguments accepted by callback should be consistent with the number of arrays passed to the Array_map () function.

The simple point is that the callback function has a final effect on each value in the array, and is reflected in the return value of the call Array_map.

This function can operate on multiple arrays at the same time. And eventually merged into an array (each array is parallel to the subscript to the same two-dimensional array).


Array_filter:

This function is dedicated to the elements in the past array, simply by filtering the value of the callback function in the array and returning the processed array via the Array_filter () function.


So the analysis to this we can be yo on a array_walk and array_map to do array preprocessing, most commonly is the trim operation on each array value. The following is the author's sample code:

The code is as follows Copy Code

<?php
$tmp = Array (' AAAA bbbb CCCC ', ' aaaa bbbb c ', ' dddddd eeeeee ');

Function Array_walk_func (& $v, $k) {
  $v = Trim ($v);
}
Array_walk ($tmp, ' array_walk_func ');
Var_dump ($tmp);

Function Array_map_func ($v) {
 return trim ($v);
}
$ret = Array_map (' Array_map_func ', $tmp);
Var_dump ($ret);


Output:

Array (3) {
  [0] =>
  String (a) "AAAA bbbb CCCC"
  [1] =>
  ; String (one) "AAAA bbbb C"
  [2] =>
  string dddddd eeeeee "
}
Array (3) {
  [0] =& Gt
  string "AAAA bbbb CCCC"
  [1] =>
  string (one) aaaa bbbb c
  [2] =>
  string "dddddd eeeeee"
}

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.