PHP - Ds Map::filter() Function



The PHP Ds\Map::filter() function is used to create a new map by using a callable function that determines which pairs to include.

The callable function is an optional parameter that returns a boolean value to indicate which pairs should be included. If this function is not provided, the filter() function creates a new map containing all the pairs that are converted to "true".

Syntax

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

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

Parameters

Following is the parameter of this function −

  • callback − An optional callable, which returns 'true', if the pair should be included, 'false' otherwise.

Return value

This function returns a new map that contains all pairs for which either a callback returns true or all values that convert to true if a callback was not provided.

Example 1

The following is the basic example of the PHP Ds\Map::filter() function −

<?php  
   $map = new \Ds\Map([1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 60]);
   echo "The original map elements: ";
   print_r($map);
   echo "The new map is: \n";
   var_dump($map->filter(function($key, $val) {  
      return $val % 15 == 0;  
   }));  
?>

Output

The above program produces the following output −

The original map elements: Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 3
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => 4
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => 5
            [value] => 60
        )

)
The new map is:
object(Ds\Map)#5 (2) {
  [0]=>
  object(Ds\Pair)#6 (2) {
    ["key"]=>
    int(3)
    ["value"]=>
    int(30)
  }
  [1]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    int(5)
    ["value"]=>
    int(60)
  }
}

Example 2

Following is another example of the PHPDs\Map::filter()function. We use this function to create a new map by using a callable function that determines which pairs will be included from the original map ([1 => "Tutorials", 2 => "Point", 3 => "India", 4 => "Tutorix", 5 => "Hyderabad", 6 => "India"]) −

<?php 
   $map = new \Ds\Map([
   1 => "Tutorials", 
   2 => "Point", 
   3 => "India", 
   4 => "Tutorix", 
   5 => "Hyderabad", 
   6 => "India"
   ]);
   echo "The original map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "\nThe new map: \n";
   var_dump($map->filter(function($key, $val) {
      return $key % 2 == 0;  
   }));  
?>

Output

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

The original map elements are:
[1] = Tutorials
[2] = Point
[3] = India
[4] = Tutorix
[5] = Hyderabad
[6] = India

The new map:
object(Ds\Map)#3 (3) {
  [0]=>
  object(Ds\Pair)#2 (2) {
    ["key"]=>
    int(2)
    ["value"]=>
    string(5) "Point"
  }
  [1]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    int(4)
    ["value"]=>
    string(7) "Tutorix"
  }
  [2]=>
  object(Ds\Pair)#5 (2) {
    ["key"]=>
    int(6)
    ["value"]=>
    string(5) "India"
  }
}

Example 3

If the callable function is omitted, this function includes all the pairs whose values are converted to true.

<?php 
   $map = new \Ds\Map([
   1 => 'a', 
   2 => 'e', 
   3 => 'i', 
   4 => 'o', 
   5 => 'u',
   ]);
   echo "The original map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "\nThe new map: \n";
   $new_map = $map->filter();
   foreach($new_map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
?>

Output

Once the above program is executed, it generates the following output −

The original map elements are:
[1] = a
[2] = e
[3] = i
[4] = o
[5] = u

The new map:
[1] = a
[2] = e
[3] = i
[4] = o
[5] = u
php_function_reference.htm
Advertisements