开发者

PHP array_filter, how to get the key in callback?

array_filter — Filters elements of an array using a callback function

array array_filter ( array $input [, callback $callback ] )

开发者_JAVA百科

Can callback get the key of the current array value and how?


From the documentation: PHP 5.6.0 Added optional flag parameter and constants ARRAY_FILTER_USE_KEY and ARRAY_FILTER_USE_BOTH

http://php.net/manual/en/function.array-filter.php


In a previous comment you outlined you're actually looking for something like this:

foreach ($t as $k => $v)
    if (!array_key_exists($k, $valid))
        unset($t[$k])

So actually to remove all values from array $t that do not have a key in array $valid.

The PHP function for that is called array_intersect_key. The intersection is equal to the filtered result:

$filtered = array_intersect_key($t, $valid);


By using the ARRAY_FILTER_USE_BOTH constant, you can get both value and key :

array_filter($arrFoo, function ($value, $key) { return 'name' === $key && $value > 1 }, ARRAY_FILTER_USE_BOTH)

By using the ARRAY_FILTER_USE_KEY constant, you can get key alone :

array_filter($arrFoo, function ($key) { return 'name' === $key }, ARRAY_FILTER_USE_KEY)


There's no way to let the callback of array_filter access the element's key, nor is there a similar function that does what you want.

However, you can write your own function for this, like the one below:

function arrayfilter(array $array, callable $callback = null) {
    if ($callback == null) {
        $callback = function($key, $val) {
            return (bool) $val;
        };
    }
    $return = array();
    foreach ($array as $key => $val) {
        if ($callback($key, $val)) {
            $return[$key] = $val;
        }
    }
    return $return;
}

$test_array = array('foo', 'a' => 'the a', 'b' => 'the b', 11 => 1101, '', null, false, 0);

$array = arrayfilter($test_array, function($key, $val) {
   return is_string($key);
});
print_r($array);
/*
Array
(
    [a] => the a
    [b] => the b
)
*/

$array = arrayfilter($test_array);
print_r($array);
/*
Array
(
    [0] => foo
    [a] => the a
    [b] => the b
    [11] => 1101
)
*/


You could use the array_walk function as discussed here (3rd answer down): is it possible if callback in array_filter receive parameter?


I didn't like the other options suggested here, if anyone else is looking for this feature, here is a quick implementation:

function array_filter_keys($a, $c){
    $i=array_filter(array_keys($a), $c);
    return array_intersect_key($a, array_flip($i));
}

The result of this function is exactly what it sounds like, it will return an array filtered by a callback function that receives the keys rather than the values.

EDIT: This is more of a disclaimer, after reading some of the other comments I realize that the OP is actually just looking for array_intersect as hakre pointed out. I will leave this answer here though since the OPs question does not clearly state that need and this page shows up in google for array_filter_keys

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜