• PHP Video Tutorials

PHP - Ds Set filter() Function



The PHP Ds\Set::filter() function is used to create a new set by using a callable to determine which values should be included in the newly created set.

The callable function is an optional parameter that returns true for the values that should be included in the set and false for the values that should not be included.

Syntax

Following is the syntax of the PHP Ds\Set::filter() function −

public Ds\Set::filter(callable $callback = ?): Ds\Set

Parameters

This function accepts a single parameter as a 'callback' function, which is described below −

  • callback − An optional callback function that returns true if the value should be included; otherwise 'false'.

Following is the syntax of the callable function −

callback(mixed $value): bool

Return value

This function returns a new set containing all values for which either the callback returned true or all values converted to true if a callback was not provided.

Example 1

The following is the basic example of the PHP Ds\Set::filter() function.

<?php
   $set = new \Ds\Set([1,2,3,4,5,6,7,8,9]);
   echo "The original set elements: \n";
   print_r($set);
   echo "The elements after the filter() function is invoked : \n";
   print_r($set->filter(function($value) {
      return ($value+3)%2 == 0;
   }));
?>

Output

The above program produces the following output −

The original set elements:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
The elements after the filter() function is invoked :
Ds\Set Object
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 9
)

Example 2

The following is another example of the PHP Ds\Set::filter() function. We use this function to create a new set using a callable function that returns the values multiples of 11.

<?php
   $set = new \Ds\Set([74, 99, 177, 66, 198, 121, 154]);
   echo "The original set elements: \n";
   print_r($set);
   echo "The elements which are divisbile by 11 are: \n";
   print_r($set->filter(function($value) {
      return $value%11 == 0;
   }));
?>

Output

After executing the above program, it will produce the following output −

The original set elements:
Ds\Set Object
(
    [0] => 74
    [1] => 99
    [2] => 177
    [3] => 66
    [4] => 198
    [5] => 121
    [6] => 154
)
The elements which are divisbile by 11 are:
Ds\Set Object
(
    [0] => 99
    [1] => 66
    [2] => 198
    [3] => 121
    [4] => 154
)

Example 3

In the example below, we use the PHP Ds\Set::filter() function to create a new set that includes only the values that are less than 50. The callback function returns true for the current set ([1, 5, 10, 15, 20]) values where the multiplication with 3 is less than 50.

<?php
   $set = new \Ds\Set([1, 5, 10, 15, 20]);
   echo "The original set elements: \n";
   print_r($set);
   echo "The elements after applying the filter() function are: \n";
   print_r($set->filter(function($value) {
      return $value*3<50;
   }));
?>

Output

On executing the above program, it generates the following output −

The original set elements:
Ds\Set Object
(
    [0] => 1
    [1] => 5
    [2] => 10
    [3] => 15
    [4] => 20
)
The elements after applying the filter() function are:
Ds\Set Object
(
    [0] => 1
    [1] => 5
    [2] => 10
    [3] => 15
)

Example 4

If the callable function returns 'false' for each value of the current set, no value will be included in the newly created set.

<?php
   $set = new \Ds\Set([1, 5, 10, 15, 20]);
   echo "The original set elements: \n";
   print_r($set);
   echo "The elements after applying the filter() function are: \n";
   print_r($set->filter(function($value) {
      return $value+($value+1) == 42;
   }));
?>

Output

Once the above program is executed, it will display the following output −

The original set elements:
Ds\Set Object
(
    [0] => 1
    [1] => 5
    [2] => 10
    [3] => 15
    [4] => 20
)
The elements after applying the filter() function are:
Ds\Set Object
(
)
php_function_reference.htm
Advertisements