PHP Array array_filter() Function (With Examples)

In this tutorial, we will learn about the PHP array_filter() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_filter() Function

The array_filter() function is used to apply a filter on array elements based on the function and returns the array with filtered elements, it accepts an array to be checked and a callback function. The callback function is used to validate the elements in the array.

Syntax

The syntax of the array_filter() function:

array_filter(array,callback_function) : array

Parameters

The parameters of the array_filter() function:

  • array is the input array in which we have to apply the filter.
  • callback_function is the function, in which we write the condition to be validated.

Return Value

The return type of this method is array, it returns the filtered array.

Sample Input/Output

Input:
$arr = array(10, 20, -10, -20, 50, 0);

//here we have to filter the positive numbers 
//the callback function to check the positive number is "isPositive()"
Function calling: 
$temp = array_filter($arr, "isPositive");

Output:
Array
(
    [0] => 10
    [1] => 20
    [4] => 50
)

So, here 0th , 1st and 4th elements are positive

PHP array_filter() Function Example 1

Find the positive number from given array of the numbers.

<?php
    //function to check wheather number is positive or not
    function isPositive($val)
    {
        if($val>0)
            return $val;
    }
    
    // array 
    $arr = array(10, 20, -10, -20, 50, 0);
    
    // array with only positive value
    $temp = array_filter($arr, "isPositive");
    
    print_r ($temp);
    
?>

Output

The output of the above example is:

Array
(
    [0] => 10
    [1] => 20
    [4] => 50
)

PHP array_filter() Function Example 2

Find the persons who are eligible for voting from given array of persons.

Here, we also have the "keys" and based on the age key we are checking the voting eligibility.

<?php
    //function to check wheather person is eligible
    //for voting or not?
    function isVoter($val)
    {
        if($val['age']>=18)
            return $val;
    }
    
    // person's array 
    $arr = array(
            array("name" => "Prem", "age" => 28,"city" => "Gwalior",),
            array("name" => "Manju", "age" => 25,"city" => "Gwalior",),
            array("name" => "Radib Kar", "age" => 23,"city" => "Chennai",),
            array("name" => "Prerana", "age" => 17,"city" => "Gwalior",),
        );
    
    // array with voting eligible persons
    $temp = array_filter($arr, "isVoter");
    
    print_r ($temp);
    
?>

Output

The output of the above example is:

Array
(
    [0] => Array
        (
            [name] => Prem
            [age] => 28   
            [city] => Gwalior
        )
         
    [1] => Array          
        (
            [name] => Manju  
            [age] => 25   
            [city] => Gwalior
        )
         
    [2] => Array          
        (
            [name] => Radib Kar 
            [age] => 23   
            [city] => Chennai
        )
         
)

To understand the above examples, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.