我得到这个名为$files[]
多个阵列,其中包括键和值如下:如果array_filter中的回调接收参数,有可能吗?
[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
)
我用这个代码创建一个新的数组由“名称”唯一密钥。但它没有
array_filter($files, "is_inarr_key('name')");
function is_inarr_key($array, $key)
{
//TODO : remove every array except those who got the same $key
}
,我得到这个错误:
array_filter() [function.array-filter]: The second argument, 'is_inarr_key('name')', should be a valid callback in C:\xampp\htdocs\php\gettingstarted\index.php on line 15
所以我的问题是:
- 是否有可能作出
array_filter
回调函数接收参数? - 关于如何在任何PHP内置函数中使用回调,一般的经验法则是什么?
我还没有尝试。但这个解决方案的brilyan。如果我将我的代码转换为闭包或类,我将能够使用更多的键并创建一个函数:is_inarr_key() – justjoe 2010-03-27 15:35:20
+1,但如果是我的答案,我会首先列出基于闭包的解决方案,并推荐它在替代品上。这是迄今为止最简单和最短的可能性。 – 2014-04-18 17:07:33