Filter array by key php

Filter array by key php

The array_filter() function filters the array’s values using the callback function. The array_filter() function passes each value of an input array to the callback function. If the callback function returns a true value, the current value from ant input is returned into the result array.

The array_filter() is a built-in PHP function that filters the values of an array using a callback function. The array_filter() method iterates over each value in an array, passing them to the callback function. If the callback function returns a true value, the current value from an array is returned to the result array. Array keys are preserved. Array keys are preserved.

How to filter array in PHP

To filter an array in PHP, use the array_filter() method. The array_filter() takes an array and filter function and returns the filtered array. The filter function is a custom user-defined function with its logic and based on that, it filters out the array values and returns them.

Let’s take an example.

In the above code, we are checking every array item that is even; if it is odd, it will be filtered out from the array, and only even item stays in the array.

The output is following.

Filter array by key php

We can also write the array_filter() function like the following.

The output will be the same as above; we have just written an anonymous function.

ARRAY_FILTER_USE_KEY

PHP 5.6 introduced the third parameter to array_filter() called flag, which you could set to ARRAY_FILTER_USE_KEY  to filter by key instead of a value. See the following example.

 1, 'b' => 2, 'c' => 3, 'd' => 4];
$outputA = array_filter($arr, function($k) {
  return $k == 'b';
}, ARRAY_FILTER_USE_KEY);
print_r($outputA);

The output is following.

Filter array by key php

ARRAY_FILTER_USE_BOTH

You can set it to ARRAY_FILTER_USE_BOTH  to filter by key or value. See the following example.

 1, 'b' => 2, 'c' => 3, 'd' => 4];

$outputB = array_filter($arr, function($v, $k) {
  return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH);
print_r($outputB);

The output is following.

Filter array by key php

Accessing Keys in array_filter() function

You can access the current key of an array by passing a reference to the array into the callback function and call key() and next() method in the callback function. See the following example.

 1, 'second' => 2, 'third' => 3);
$data = array_filter($data, function ($item) use (&$data) {
    echo "Filtering key ", key($data)."\n";
    next($data);
});

The output is following.

Filter array by key php

That’s it for this example.

How to filter array based on key PHP?

Filtering a PHP array by keys To use the PHP array_filter() function to filter array elements by key instead of value, you can pass the ARRAY_FILTER_USE_KEY flag as the third argument to the function. This would pass the key as the only argument to the provided callback function.

What are PHP filters?

PHP filters are used to validate and sanitize external input. The PHP filter extension has many of the functions needed for checking user input, and is designed to make data validation easier and quicker.

Is PHP an array?

Definition and Usage. The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.