Change language

PHP Ds / Sequence () filter function

|
Syntax:
DsSequence abstract public DsSequence::filter ([callable $callback])
Parameter:This parameter is optional and it returns True if the value should be included, otherwise False.Return value:This function returns a new sequence containing all values for which the callback returned True or all values ​​that convert to True if no callback was provided.The programs below illustrate the Ds / Sequence::filter()function in PHP:Example 1:  
// Create a new sequence $seq = new DsVector ([10, 20, 30, 40, 50]);  
// Display the new sequence using the filter function var_dump ( $seq -> filter ( function ( $val ) { return $val % 4 == 0; }));  
?>
Output:
object (DsVector) # 3 (2) {[0] = > int (20) [1] = > int (40)}
Example 2:  
// Create a new sequence $seq = new DsVector ([2, 5 , 4, 8, 3, 9]);  
// Display the new sequence using the filter function var_dump ( $seq -> filter ( function ( $val ) { return $val ; }));  
?>
Output:
object (DsVector) # 3 (6) {[0] = > int (2) [1] = > int (5) [2] = > int (4) [3] = > int (8) [4] = > int (3) [5] = > int (9)}
Link: http: // php .net / manual / en / ds-sequence.filter.php