PHP | array_filter() Function

This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback function returns true then the current value of the array is returned into the result array otherwise not. This way the keys of the array gets preserved, i.e. the key of element in the original array and output array are same.

Syntax:

array array_filter($array, $callback_function, $flag)

Parameters: The function takes three parameters, out of which one is mandatory and the other two are optional.

  1. $array (mandatory): This refers to the input array on which the filter operation is to be performed.
  2. $callback_function (optional): Refers to the user-defined function. If the function is not supplied then all entries of the array equal to FALSE , will be removed.
  3. $flag (optional): Refers to the arguments passed to the callback function.
    • ARRAY_FILTER_USE_KEY – passes key as the only argument to a callback function, instead of the value of the array.
    • ARRAY_FILTER_USE_BOTH – passes both value and key as arguments to callback instead of the value.

Return Value: The function returns a filtered array.

Below is a program showing how to return or filter out even elements from an array using array_filter() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP function to check for even elements in an array
function Even($array)
{
    // returns if the input integer is even
    if($array%2==0)
       return TRUE;
    else 
       return FALSE; 
}
  
$array = array(12, 0, 0, 18, 27, 0, 46);
print_r(array_filter($array, "Even"));
  
?>

chevron_right


Output:

Array
(
    [0] => 12
    [1] => 0
    [2] => 0
    [3] => 18
    [5] => 0
    [6] => 46
)

In this example, we will not pass the callback function and let’s see the output. We will see that the 0 or false elements are not printed:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP function to check for even elements in an array
function Even($array)
{
    // returns if the input integer is even
    if($array%2==0)
       return TRUE;
    else 
       return FALSE; 
}
  
$array = array(12, 0, 0, 18, 27, 0, 46);
print_r(array_filter($array));
  
?>

chevron_right


Output:

Array
(
    [0] => 12
    [3] => 18
    [4] => 27
    [6] => 46
)

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.