首页 > 代码库 > PHP Array_Map

PHP Array_Map

<?phpfunction 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 )

<?phpfunction 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 ))

PHP Array_Map