A brief analysis of the use of Array_map and Array_walk in PHP _php skills

Source: Internet
Author: User

One, Array_map ()

1 array_map() . The function takes a user-defined function to each value in the array and returns an array with the new value after the user-defined function, if the function does not return a value, then the corresponding new value array is empty.

2. The number of arguments accepted by the callback function should be the array_map() same as the number of arrays passed to the function.

3. Hint: You can enter one or more arrays to the function.

If the phase function Input 2 array, its function accepts the parameter also should have two, when the map gives the function to pass the value, is each time from two array each to take one to transmit the letter
Number. That is, multiple arrays are submitted synchronously, rather than submitting an array and submitting the next commit several arrays, the function also needs to have several arguments

4. Grammar:array array_map ( callable callback,arrayarr1 [, array $… ] )

Instance:

<?php 
//singular group form
function MyFunction ($v)
{
 if ($v = = "Dog")
 {return
  "Fido";
 }
 return $v;
}

$a =array ("Horse", "Dog", "Cat");
Print_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");
Print_r (Array_map ("MyFunction1", $a 1, $a 2));
>

Second, Array_walk ()

1, array_walk-use user-defined function in the array of each element to do callback processing

2. Grammar: bool array_walk ( array &array,callablefuncname [, mixed $userdata = NULL ] )

Parameters:

The array $array input.

$funcname A callback function, typically $funcname accept two parameters. $array the value of the parameter as the first, and the key 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 act directly on the values in the array, the first argument to FuncName is specified as a reference (add & sign). Such
Any changes to these units 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");
The passed parameter group
function 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. Lemon
A. Orange
B. Banana c. And after
 :
d. Fruit:lemon
A. Fruit:or Ange
B. Fruit:banana
c. fruit:apple

Key points:

The map is primarily designed to get the new array of your callback functions, which is the result.

The main walk is to use your callback function once for each parameter, which is the process of processing.

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

Walk is primarily to operate on each value within an array, and the result of the operation affects the original arrays

The map is primarily an array of values that are manipulated to return to a new array

Walk can have no return value map to have, because to populate the array

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.