PHP Array_Map

<?php
function myfunction($v) 
{
if ($v==="Dog")
    {
    return "Fido";
    }
return $v;
}
$a=array("Horse","Dog","Cat");
print_r(array_map("myfunction",$a));
?>

输出:

Array ( [0] => Horse [1] => Fido [2] => Cat )

<?php
function myfunction($v1,$v2) 
{
if ($v1===$v2)
    {
    return "same";
    }
return "different";
}
$a1=array("Horse","Dog","Cat");
$a2=array("Cow","Dog","Rat");
print_r(array_map("myfunction",$a1,$a2));
?>

输出:

Array ( [0] => different [1] => same [2] => different )

请看当自定义函数名设置为 null 时的情况:
<?php
$a1=array("Dog","Cat");
$a2=array("Puppy","Kitten");
print_r(array_map(null,$a1,$a2));
?>

输出:

Array (
[0] => Array ( [0] => Dog [1] => Puppy )
[1] => Array ( [0] => Cat [1] => Kitten )
)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。