Skip to content
Related Articles

Related Articles

PHP | Ds\Sequence filter() Function
  • Last Updated : 21 Aug, 2019

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :