First, Array_map ()
1 array_map()
. The function functions the user-defined function to each value in the array, and returns an array with the new value after the function of the user-defined function, and if there is no return value after functions, 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 array_map()
the 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 after committing an array and committing the next commit several arrays, the function also needs to have several parameters
4. Syntax:array array_map ( callable callback,arrayarr1 [, array $… ] )
Instance:
|
<?php
//单数组形式
function myfunction(
$v
)
{
if (
$v
===
"Dog"
)
{
return "Fido"
;
}
return $v
;
}
$a
=
array
(
"Horse"
,
"Dog"
,
"Cat"
);
print_r(
array_map
(
"myfunction"
,
$a
));
//多数组形式
function myfunction1(
$v1
,
$v2
)
{
if (
$v1
===
$v2
)
{
return "same"
;
}
return "different"
;
}
$a1
=
array
(
"Horse"
,
"Dog"
,
"Cat"
);
$a2
=
array
(
"Cow"
,
"Dog"
,
"Rat"
);
print_r(
array_map
(
"myfunction1"
,
$a1
,
$a2
));
?>
|
Comparison of use of Array_map and Array_walk in PHP