Ds\Map::filter()函数是PHP中的内置函数, 用于使用filter函数创建新地图。
语法如下:
Ds\Map public Ds\Map::filter( $callback ) 
 
 
参数:它包含一个参数$ callback这是一个可选参数, 如果应包含该值, 则返回True, 否则返回False。
返回值:此函数返回一个新的映射, 其中包含回调返回True的所有对或未提供回调的所有转换为True的值。
下面的程序说明了Ds\Map::filter()PHP中的功能:
程序1:
<?php 
//PHP program to illustrate the filter() 
//function of Ds\map 
  
//Creating a Map 
$map = new \Ds\Map([
     1 => "Welcome" , 2 => "to" , 3 => "Geeks" , 4 => "for" , 5 => "Geeks" ]);
              
  
//Display new sequence using filter function 
var_dump( $map ->filter( function ( $key , $val ) { 
     return $key % 3 == 0; 
})); 
  
?> 
 
 
输出如下:
object(Ds\Map)#3 (1) {
  [0]=>
  object(Ds\Pair)#2 (2) {
    ["key"]=>
    int(3)
    ["value"]=>
    string(5) "Geeks"
  }
} 
 
 
程序2:
<?php 
//PHP program to illustrate the filter() 
//function of Ds\map 
  
//Creating a Map 
$map = new \Ds\Map([
         1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 50]); 
              
//Display new sequence using filter function 
var_dump( $map ->filter( function ( $key , $val ) { 
     return $val % 20 == 0; 
})); 
  
?> 
 
 
输出如下:
object(Ds\Map)#3 (2) {
  [0]=>
  object(Ds\Pair)#2 (2) {
    ["key"]=>
    int(2)
    ["value"]=>
    int(20)
  }
  [1]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    int(4)
    ["value"]=>
    int(40)
  }
} 
 
 
参考: https://www.php.net/manual/en/ds-map.filter.php

