作者:梁琦rx1987_865 | 来源:互联网 | 2014-03-29 12:37
为什么回调函数在array_filter()里面没有起作用?<?phpfunction myFunction($v) {if ($v==="Dog"){return "Fido";}return $v;
为什么回调函数在array_filter()里面没有起作用?
function myFunction($v)
{
if ($v==="Dog")
{
return "Fido";
}
return $v;
}
$a=array("Horse","Dog","Cat");
//Array ( [0] => Horse [1] => Fido [2] => Cat )
print_r(array_map("myFunction",$a));
//Array ( [0] => Horse [1] => Dog [2] => Cat )
//为什么回调函数在array_filter()里面没有起作用?
print_r(array_filter($a,"myFunction"));
?>
------解决方案--------------------去看下array_filter 的用法就会明白
------解决方案--------------------function myFunction($v)
{
if ($v==="Dog")
{
return true;
}
return false;
}
$a=array("Horse","Dog","Cat");
//Array ( [0] => Horse [1] => Fido [2] => Cat )
print_r(array_map("myFunction",$a));
//Array ( [0] => Horse [1] => Dog [2] => Cat )
//为什么回调函数在array_filter()里面没有起作用?
print_r(array_filter($a,"myFunction"));