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:
<?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; })); ?> |
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:
<?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; })); ?> |
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