The difference between the usage of Array_map function and Array_walk function in PHP

Source: Internet
Author: User

This article brings the content is about PHP array_map function and the use of array_walk function difference, there is a certain reference value, the need for friends can refer to, I hope to help you.

Array_map ():
1. The Array_map () function functions The user-defined function to each value in the array, and returns an array with the new value after the user-defined function, and if there is no return value after the function, the corresponding new value array is empty.
2. The number of arguments that the callback function accepts should be the same as the number of arrays passed to the Array_map () function.
3. Tip: You can enter one or more arrays into the function.

If the phase function input 2 arrays, its function accepts the parameter also should have two, the map gives the function to pass the value, is each time from the two array each one transmits the letter
Number. That is, multiple arrays are committed synchronously, not one array is submitted and the next
To commit several arrays, the function also needs to have several parameters

4. Syntax: Array array_map (callable arr1 [, Array $ ...])

Instance:

<?php//Singular group form function MyFunction ($v) {    if ($v = = = "Dog")    {            return "Fido";    }        return $v;} $a =array ("Horse", "Dog", "Cat");p Rint_r (Array_map ("MyFunction", $a));//most group form function MyFunction1 ($v 1, $v 2) {    if ( $v 1=== $v 2)    {            return "Same";    }        return "Different";} $a 1=array ("Horse", "Dog", "Cat"), $a 2=array ("Cow", "Dog", "Rat");p Rint_r (Array_map ("MyFunction1", $a 1, $a 2));? >

Array_walk ():
1. array_walk-uses user-defined functions to do callback processing for each element in the array
2. Syntax: BOOL Array_walk (Array &

FuncName [, Mixed $userdata = NULL])

Parameters:

$array the input array. $funcname A callback function, typically $funcname accept two parameters.

$array the value of the parameter as the first, the key name as the second.

$userdata If an optional parameter $userdata is provided, it is passed as the third argument to the $funcname.

Attention:

If $funcname need to directly act on the values in the array, the first argument to FuncName is specified as a reference (add & symbol). Such
Any changes to these cells will also change the original array itself.

return value:

Returns TRUE on success, or FALSE on failure.

Instance:

<?php$fruits = Array ("d" = "lemon", "a" = "orange", "b" = "banana", "c" = "apple");//pass reference, change the passed parameter group Functi On Test_alter (& $item 1, $key, $prefix) {    $item 1 = "$prefix: $item 1";} function Test_print ($item 2, $key) {    echo "$key. $item 2<br/>\n ";} echo "before ...: \ n";//singular group array_walk ($fruits, ' test_print ');//with extra parameters Array_walk ($fruits, ' test_alter ', ' fruit '); echo " ... and after:\n "; Array_walk ($fruits, ' test_print ');

The above routines will output:

Before.:d. Lemona. Orangeb. Bananac. Apple ... and after:d. Fruit:lemona. Fruit:orangeb. Fruit:bananac. Fruit:apple

Key points:

The map is basically to get the new array after your callback function is processed, the result.

Walk is mainly used once for each parameter of your callback function, to be the process of processing.

Walk can be considered to provide additional parameters to the callback function, map can not

Walk is mainly to operate on each value in the array, the result of the operation affects the original arrays map is mainly the value of the array in the operation after the return of arrays, to get a new array

Walk can have no return value map to have because the array to be populated

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.