Ds \ Set::filter()函數是PHP中的內置函數,用於使用過濾器函數創建新集合。
用法:
Ds\Set public Ds\Set::filter( $callback )
參數:該函數接受單個參數$callback,該參數是可選的,如果應包含該值,則返回True,否則返回False。
返回值:此函數返回一個新集合,該集合包含回調返回True的所有值或如果未提供回調則轉換為True的所有值。
以下示例程序旨在說明PHP中的Ds \ Set::filter()函數:
程序1:
<?php
// Create new set
$set = new \Ds\Set([10, 20, 30, 40, 50]);
// Display new set using filter function
var_dump($set->filter(function($val) {
return $val % 4 == 0;
}));
?>
輸出:
object(Ds\Set)#3 (2) {
[0]=>
int(20)
[1]=>
int(40)
}
程序2:
<?php
// Create new set
$set = new \Ds\Set([2, 5, 4, 8, 3, 9]);
// Display new set using filter function
var_dump($set->filter(function($val) {
return $val;
}));
?>
輸出:
object(Ds\Set)#3 (6) {
[0]=>
int(2)
[1]=>
int(5)
[2]=>
int(4)
[3]=>
int(8)
[4]=>
int(3)
[5]=>
int(9)
}
參考: https://www.php.net/manual/en/ds-set.filter.php
相關用法
- PHP Ds\Map filter()用法及代碼示例
- PHP Ds\Vector filter()用法及代碼示例
- PHP Ds\Deque filter()用法及代碼示例
- PHP Imagick filter()用法及代碼示例
- PHP Ds\Sequence filter()用法及代碼示例
- CSS filter屬性用法及代碼示例
- CSS backdrop-filter用法及代碼示例
- ES6 Array filter()用法及代碼示例
注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | Ds\Set filter() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
