Friday, October 10, 2025
HomeLanguagesPHP | DsDeque filter() Function

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:




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


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:




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


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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS