19

array_filter — Filters elements of an array using a callback function

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

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

6 Answers 6

19

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

Sign up to request clarification or add additional context in comments.

Comments

9

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);

Comments

8

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)

Comments

3

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
)
*/

Comments

0

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

5 Comments

Too tricky, and i'm working with indexed arrays not associative ones. In that solution keys are hardcoded.
Perhaps a more standard codeforeach ($arr as $key => $val) {} implementation would serve your needs.
I need to filter the input array: if key exists in another array then return the element. I found array_filter the candidate but i can't get the key of the current array value in callback.
That is: foreach ($t as $k => $v) if (!array_key_exists($k, $valid)) unset($t[$k]) but looks horrible to me...
That's pretty much how I'd go about implementing what you've described so far.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.