PHP | Ds\Sequence filter() Function

The Ds\Sequence::filter() function is an inbuilt function in PHP which is used to create new sequence using filter function.

Syntax:

Ds\Sequence abstract public Ds\Sequence::filter ([ callable $callback ] )

Parameter: It is an optional parameter and it returns True if the value should be included, False otherwise.



Return value: This function returns a new sequence containing all the values for which either the callback returned True or all values that convert to True if a callback was not provided.

Below programs illustrate the Ds\Sequence::filter() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new sequence
$seq = new \Ds\Vector([10, 20, 30, 40, 50]);
  
// Display new sequence using filter function
var_dump($seq->filter(function($val) {
    return $val % 4 == 0;
}));
  
?>

chevron_right


Output:

object(Ds\Vector)#3 (2) {
  [0] => int(20)
  [1] => int(40)
}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create new sequence
$seq = new \Ds\Vector([2, 5, 4, 8, 3, 9]);
  
// Display new sequence using filter function
var_dump($seq->filter(function($val) {
    return $val;
}));
  
?>

chevron_right


Output:

object(Ds\Vector)#3 (6) {
  [0] => int(2)
  [1] => int(5)
  [2] => int(4)
  [3] => int(8)
  [4] => int(3)
  [5] => int(9)
}

Reference: http://php.net/manual/en/ds-sequence.filter.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.