PHP | Ds\Deque filter() Function

The Ds\Deque::filter() function is an inbuilt function in PHP which is used to filter out the elements from the deque based on the operation defined in the callback function.

Syntax:

public Ds\Deque::filter( $callback ) : Ds\Deque

Parameters: This function accepts single parameter $callback which is the callback function which contains the definition of filter to the elements from the deque.



Return Value: This function returns a new Deque which contains all the values for which callback returns True or all values that convert to True if a callback was not provided.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Creating a deque
$deque = new \Ds\Deque([1, 2, 3, 4, 5, 6]);
  
echo("Elements in the deque are\n");
  
print_r($deque);
  
// Use filter() function to filter 
// the elements as per requirement
print_r($deque->filter(function($value) {
    return $value % 2 == 0;
}));
  
?>

chevron_right


Output:

Elements in the deque are
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Ds\Deque Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Creating a deque
$deque = new \Ds\Deque([10, 20, 3, 40, 50, 6]);
  
echo("Elements in the deque are\n");
  
print_r($deque);
  
// Use filter() function to filter 
// the elements as per requirement
print_r($deque->filter(function($value) {
    return $value % 10 != 0;
}));
  
?>

chevron_right


Output:

Elements in the deque are
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 3
    [3] => 40
    [4] => 50
    [5] => 6
)
Ds\Deque Object
(
    [0] => 3
    [1] => 6
)

Reference: http://php.net/manual/en/ds-deque.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.