How Does array_filter() Work In PHP?

How Does array_filter() Work In PHP?

When using array_filter(), you provide a callback function that determines whether each element of the array should be included in the filtered result.

The callback function should return a boolean value:

  • true indicates that the element should be included in the filtered result.

  • false indicates that the element should be excluded from the filtered result.

Here's the general syntax of array_filter():

array_filter(array $array, callable $callback, int $flag = 0): array

The $array parameter is the input array you want to filter. The $callback parameter is the callback function that determines the filtering logic. The optional $flag parameter specifies additional flags for the filtering process.

By default, when you use array_filter(), the callback function only receives the value of each array element as an argument and returns true or false based on the filtering condition you define.

For example, let's say you have an array of numbers and you want to filter out all numbers greater than 5:

$numbers = [2, 8, 3, 6, 7, 1, 9, 4, 5];

$filteredNumbers = array_filter($numbers, function ($value) {
    return $value > 5;
});

print_r($filteredNumbers);

Output:

Array
(
    [1] => 8
    [3] => 6
    [4] => 7
    [6] => 9
)

In this example, the callback function checks if each number is greater than 5. If the condition is true, the number is included in the filtered result. If the condition is false, the number is excluded.

You can customize the filtering logic in the callback function according to your specific requirements.

Remember that array_filter() creates a new array with the elements that pass the filtering condition. The original array remains unchanged.

ARRAY_FILTER_USE_KEY

The ARRAY_FILTER_USE_KEY flag can be used with the array_filter() to provide the key of each array element to the callback function, rather than the value.

$array = array(
    'name' => 'John',
    'age' => 25,
    'country' => 'USA',
    'email' => 'john@example.com'
);

// Filter the array based on the keys
$filtered = array_filter($array, function ($key) {
    return strlen($key) > 3; // Filter keys with length greater than 3
}, ARRAY_FILTER_USE_KEY);

print_r($filtered);

Output:

Array
(
    [name] => John
    [country] => USA
    [email] => john@example.com
)

In this example, ARRAY_FILTER_USE_KEY is used, and the filtering callback function receives only the keys of the array elements. It filters out the array elements whose key lengths are greater than 3.

ARRAY_FILTER_USE_BOTH

The ARRAY_FILTER_USE_BOTH flag can also be used with array_filter() to provide both the key and value of the current array element to the callback function.

However, when you specify the ARRAY_FILTER_USE_BOTH flag, the callback function will receive both the key and value as arguments.

Here's an example to illustrate the usage of ARRAY_FILTER_USE_BOTH:

$fruits = [
    'apple' => 2,
    'banana' => 1,
    'orange' => 3,
    'mango' => 4,
];

$filteredFruits = array_filter($fruits, function ($value, $key) {
    return $value >= 3 && strlen($key) >= 5;
}, ARRAY_FILTER_USE_BOTH);

print_r($filteredFruits);

Output:

Array
(
    [orange] => 3
    [mango] => 4
)

In this example, the callback function receives both the key and value of each array element. The condition in the callback checks if the value is greater than or equal to 3 and if the length of the key is greater than or equal to 5. If both conditions are true, the element is included in the filtered result.

By using the ARRAY_FILTER_USE_BOTH flag, you have access to both the key and value, allowing you to create more complex filtering conditions based on various criteria.

It's worth noting that when using ARRAY_FILTER_USE_BOTH, the callback function should accept two parameters (one for the value and one for the key), even if you don't use one or both of them in your filtering logic.