ryb*_*ome 0 php reduce closures anonymous-function
我正在尝试执行以下PHP代码:
$path_hierarchy = // function that returns an array
return array_reduce(
$terms,
function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
}
);
Run Code Online (Sandbox Code Playgroud)
...但是我收到以下PHP错误:
PHP Parse error: syntax error, unexpected ')', expecting '{'
所以,我切换到以下语法:
$path_hierarchy = // function that returns an array
$callback = function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
};
return array_reduce(
$terms,
$callback
);
Run Code Online (Sandbox Code Playgroud)
......这很有效.我不能use在匿名函数的上下文中使用关键字作为另一个函数的参数吗?