array_filter(callable, array1) is another very powerful function available in PHP. Basically is a way to remove elements from an array that does not pass given rules in the callable function. These are the most common ways to implement this function:

Option 1. Use a callable outside array_filter function. Declare a function and call it inside the array_filter function:

/**
 * @param int $item
 * @return bool
 */
function removeOddsFromList(int $item): bool
{
    return $item % 2 == 0;
}

$array = [0,1,2,3,4,5,6,7,8,9,10,11,12,3,14,15,56,78,89,45,103,210,211];

$result = array_filter($array, 'removeOddsFromList');

Option 2. Use anonymous function inside the array_filter function:

$array = [0,1,2,3,4,5,6,7,8,9,10,11,12,3,14,15,56,78,89,45,103,210,211];

$result = array_filter($array, function (int $item): bool {return $item % 2 == 0;});

In both cases the output is an associative array corresponding to each one of the elements that remain in the array:

Array
(
    [0] => 0
    [2] => 2
    [4] => 4
    [6] => 6
    [8] => 8
    [10] => 10
    [12] => 12
    [14] => 14
    [16] => 56
    [17] => 78
    [21] => 210
)

Only the elements that evaluate to true in the call back are returned, in this case, removing all odd numbers. The original array keys are preserved in the result.

Option 3. use array_filter with mode.

$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];

$result_1 = array_filter($arr, function($k) {return $k == 'd';}, ARRAY_FILTER_USE_KEY);

$result_2 = array_filter($arr, function($v, $k) {return $k == 'f' || $v == 2;}, ARRAY_FILTER_USE_BOTH);

The output of $result_1 and $result_2 will be:

Array
(
    [d] => 4
)


Array
(
    [b] => 2
    [f] => 6
)

 Notice that using

mode = ARRAY_FILTER_USE_KEY

 will make the callable to accept the keys of the array while doing

mode = ARRAY_FILTER_USE_BOTH

 will make the callable to accept both the key - value pair of each element of the array.

Option 4. apply array_filter without specifying a callback function. This is very useful if we want to remove all empty entries from the array:

$arr = [
    0 => 'im not empty',
    1 => false,
    2 => -1,
    3 => null,
    4 => '',
    5 => '0',
    6 => 0,
];

$result = array_filter($arr);

The output will be:

Array
(
    [0] => im not empty
    [2] => -1
)

 In this case, all values that validate to empty (refer to empty function in PHP Documentation) are removed from the result.

 For more on this refer to array filter in PHP Documentation.