PHP array_filter function With Examples

The PHP array_filter() is a built-in PHP function that filters the array values using a callback function.

In this article, we will talk about PHP array filter in a detailed explanation as well as example programs that could be helpful to your project development. This topic is a continuation of the previous article, entitled PHP Multiline String.

What is Array_filter PHP?

In PHP, array_filter function can be used to filter a value of the array using a function name (callback). This function moves every value of the array input to the callback function. Once the callback function returns true, the present value from the input will be returned into the array result.

Syntax

array_filter(array, callback_function, flag)

Parameter values

ParameterDescription
arrayThis array is a required parameter that specifies the array to be filtered and also refers to an input array in which all the operations are performed.
callback_functionThis callback function is an optional parameter which specifies the callback function that will be used. This also referred to a function named (user-defined). Once the function will not provide it means that all the entries of the array are returned equally to false and will be removed.
flagThis flag is an optional parameter which specifies which arguments will be sent to the callback.

ARRAY_FILTER_USE_KEY – use to pass a key as one argument to a function name (callback), rather than a value in an array.

ARRAY_FILTER_USE_BOTH – use to pass both key and value as an argument to a callback function rather on the value.

Example

<?php

      // PHP function to check for even elements in an array
      function Even($array)
      {
      	// returns if the input integer is even and its original array
      	if($array%2==0)
      	return TRUE;
      	else
      	return FALSE;
      }
      
      $array = array(26, 10, 15, 11, 29, 0, 86);
      print_r(array_filter($array, "Even"));

?>

Output

Array
(
    [0] => 26
    [1] => 10
    [5] => 0
    [6] => 86
)

More PHP array_filter example programs

The following program could help you understand an array_filter in the easiest and better way.

1. Basic example program for PHP array_filter function

Filter out all the Odd numbers in the given array passing elements.

Example

<?php

    $given_Numbers = [1, 2, 3, 4, 5];
    
    $even_numbers = array_filter(
    	$given_Numbers,
    	function ($given_Numbers) {
    		return $given_Numbers % 2 === 0;
    	}
    );

print_r($even_numbers);

Output

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

2. Example program using a callback function as a method of the class

The odd class has a function name isOdd() which returns true once the argument is an odd number or else it returns false.

Example

<?php

class Odd
{
	public function isOdd($num)
	{
		return $num % 2 === 1;
	}
}

$numbers = [1, 2, 3, 4, 5];
$odd_numbers = array_filter(
	$numbers,
	[new Odd(), 'isOdd']
);

print_r($odd_numbers);

Output

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

3. Example program for passing an element to the callback function

Given on the array_filter function passing the value of every element of an array using a callback function for filtering.

In some cases, if you want to pass the filtering key and not the value to the function name callback. The solution to this in order to pass we need to use the ARRAY_FILTER_USE_KEY as the third function of the argument of the result array_filter() user-defined function.

Example

<?php

$info = [
	'firstname' => 'Glenn',
	'middlename' => 'Magada',
	'lastname' => 'Azuelo',
	'password' => 'secret',
	'email' => '[email protected]'
];

// filter use key pass to filter elements
$filtered = array_filter(
	$info,
	fn ($key) => $key !== 'password',
	ARRAY_FILTER_USE_KEY
);

print_r($filtered);

Output

Array
(
    [firstname] => Glenn
    [middlename] => Magada
    [lastname] => Azuelo
    [email] => [email protected]
)

How do you filter an array key?

To filter in array keys we use the array_filter() function in PHP and pass the ARRAY_FILTER_USE_KEY flag as the 3rd (argument) to the anonymous function.

This could allow the key to pass as only one argument on the provided (callback function) which arrays keys are preserved.

Why filters are used in PHP?

The filters are used for sanitizing and validating external inputs. This filter extension provides many functions that can be needed for checking user input and it was created for easy and better data validation.

How many filters are in PHP?

There are only 2 types of data filtering:

  • Sanitization
  • Validation

More about arrays

Summary

In summary, you have learned about PHP array_filter. This article also discussed what is Array_filter, how you filter an array key, why filters are used, and how many filters are in PHP.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment