2010-03-27 51 views
13

我得到这个名为$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

所以我的问题是:

  1. 是否有可能作出array_filter回调函数接收参数?
  2. 关于如何在任何PHP内置函数中使用回调,一般的经验法则是什么?

回答

42

您可以在PHP≥5.3上创建闭包。

array_filter($files, function($array) use ($key) { 
    return is_inarr_key($array, $key); 
}); 

如果你被卡住PHP < 5.3,...

您可以$key一个全局变量。

function is_inarr_with_global_key($array) { 
    global $key; 
    .... 
} 

您可以创建一个类

class KeyFilter { 
    function KeyFilter($key) { $this->key = $key; } 
    function is_inarr_key($array) { ... } 
} 
... 
array_filter($files, array(new KeyFilter('name'), 'is_inarr_key')); 

您可以创建3个不同的功能

array_filter($files, 'is_inarr_name'); 
array_filter($files, 'is_inarr_path'); 
array_filter($files, 'is_inarr_number'); 

您可以编写自己的array_filter

function my_array_filter($files, $key) { 
    ... 
} 
+0

我还没有尝试。但这个解决方案的brilyan。如果我将我的代码转换为闭包或类,我将能够使用更多的键并创建一个函数:is_inarr_key() – justjoe 2010-03-27 15:35:20

+3

+1,但如果是我的答案,我会首先列出基于闭包的解决方案,并推荐它在替代品上。这是迄今为止最简单和最短的可能性。 – 2014-04-18 17:07:33

1

,我不知道你是否能提供与参数的回调函数,但作为一个建议,你可以定义一个回调函数array_filter

array_filter($files, "is_inarr_key"); 

并获得数组的name,只是环通过$file[]并检索name.然后,拥有name您可以继续您的逻辑。

HTH。

2

您可以使用array_walk功能:

$arr = array(
     array("name" => "Jack", "id" => 1), 
     array("name" => "Rose", "id" => 2), 
    ); 

$result = array(); // initialize result array. 
array_walk($arr, "filter"); // iterate over the array calling filter fun for each value. 
// $result will now have elements "Jack" and "Rose" 

function filter($a) { 

    global $result; // to access the global result array. 

    $result[] = $a['name']; // for each array passed, save the value for the key 'name' in the result array. 
} 
1

可以在5.2.x

$y = 5; 
$func = 'return $x > ' . $y . ';'; 
$f = create_function('$x', $func); 
$numbers = range(0, 10); 
$result = array_filter($numbers, $f); 
print_r($result); 

其输出

Array ( [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 )

1

感谢KennyTM为keyfilter class great tip使用create_function()。 对于那些关心和不知道如何去做的人来说,这是一个有效的详细例子。 我稍微改进了使用正则表达式模式。

$ files [] = {code above};

class KeyFilter { 
    function KeyFilter($attr,$pattern) 
    { 
     $this->attr=$attr; 
     $this->pattern=$pattern; 
    } 
    function is_inarr_key($t) 
    { 
     return preg_match($this->pattern,$t[$this->attr]);; 
    } 
} 


$t=array_filter($items, array(new KeyFilter("path","~\d{2}\.php~i"), 'is_inarr_key')); 
print_r($t); 
print_r($o->key); 

输出:

[1] => Array 
(
    [name] => index**10**.php 
    [path] => http://localhost/php/gettingstarted/ 
    [number] => 2 
) 

[2] => Array 
(
    [name] => index**11**.php 
    [path] => http://localhost/php/gettingstarted/ 
    [number] => 3 
) 
相关问题