Friday, September 19, 2025
HomeLanguagesPHP | DsSequence filter() Function

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:




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


Output:

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

Example 2:




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


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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32300 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS