PHP

PHP array_filter

Introduction

array_filter is a function in PHP that is used to filter the array.

If there are any empty string values or null values or false values so the array_filter function will filter those empty values and make the new array with the remaining values.

Those values which did not store in an array and we want to remove them so we can easily remove them with the help of using the array_filter function.

It works excellently in the one-dimensional array if you want to use the array_filter function on the two-dimensional array or multidimensional array, you have to create a recursive function with an array filter to execute it.

Let’s have some examples.

Example 1

<?php

$fil = [
    0 => 'hoor',
    1 => null,
    2 => '',
    3 => false,
    4 => -1,
    5 => '0',
    6 => 0,
];

$filtered=(array_filter($fil));

echo "<pre>";

print_r($fil);

print_r($filtered);
?>

In the above code, we store some empty and non-empty string values in $fil array then we print the array using the array_filter method which will remove the empty strings and will give a new array with the remaining stored string values see the output below:

Output

Array
(
    [0] => hoor
    [1] => 
    [2] => 
    [3] => 
    [4] => -1
    [5] => 0
    [6] => 0
)
Array
(
    [0] => hoor
    [4] => -1
)

As you can see in the output all stored empty string values have been removed and a new array with stored non-empty string values has been created.

Example 2

<?php

function even($value)
{
  return $value % 2 == 0;
}

$fil = [1, 2, 3, 4, 5];

$filtered=(array_filter($fil,"even"));

echo "<pre>";

print_r($fil);

print_r($filtered);

?>

In the above example, we created a function for filtering even values then we declare an array and stored some integer values in it. 

The even function will find even values in an array and then the array_filter method will filter the array and will remove odd values and will create a new array with even values.

 see the output below:

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
    [1] => 2
    [3] => 4
)

As you can see in the output the odd values have been removed only even values are stored in the new array.

Example 3

<?php

function odd($value)
{
  return $value & 1;
}

$fil = [1, 2, 3, 4, 5];

$filtered=(array_filter($fil,"odd"));

echo "<pre>";

print_r($fil);

print_r($filtered);
?>

In the above code, we create a function for odd values by using the array_filter method even values will be removed from the array and only odd values will be displayed in the new array.

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
    [0] => 1
    [2] => 3
    [4] => 5
)

As you can see the output only odd values are showing in the new array and the even values have been filtered out.

Conclusion

In this tutorial, we learn PHP array_filter Method. we see the introduction and discuss why this function is actually used for. Then we discuss an example using the array filter method for filtering the array values, then we discuss another example of the array_filter Method for filtering even values from the array, and last we discuss the example of the array_filter Method for filtering odd values from the array.

Suggested Article:

Leave a Comment