我有以下代码:
function lower_than_10($i) {
return ($i < 10);
}
Run Code Online (Sandbox Code Playgroud)
我可以用来过滤这样的数组:
$arr = array(7, 8, 9, 10, 11, 12, 13);
$new_arr = array_filter($arr, 'lower_than_10');
Run Code Online (Sandbox Code Playgroud)
如何向lower_than_10添加参数,以便它还接受要检查的数字?就像,如果我有这个:
function lower_than($i, $num) {
return ($i < $num);
}
Run Code Online (Sandbox Code Playgroud)
如何从array_filter调用它传递10到$ num或任何数字?
我得到了这个多个数组$files[],它由键和值组成,如下所示:
[0] => Array
(
[name] => index1.php
[path] => http://localhost/php/gettingstarted/
[number] => 1
)
[1] => Array
(
[name] => index10.php
[path] => http://localhost/php/gettingstarted/
[number] => 2
)
[2] => Array
(
[name] => index11.php
[path] => http://localhost/php/gettingstarted/
[number] => 3
)
Run Code Online (Sandbox Code Playgroud)
我使用此代码创建一个仅由'name'键组成的新数组.但它失败了
array_filter($files, "is_inarr_key('name')");
function is_inarr_key($array, $key)
{
//TODO : remove every array except those who got the same $key
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
array_filter()[function.array滤波器]:第二个参数, 'is_inarr_key(' 姓名 ')',应该在C有效的回调:\ XAMPP\htdocs中\ PHP\gettingstarted \第15行的index.php
所以我的问题是:
array_filter接收参数上进行回调功能?