PHP | Ds\Vector filter() Function Last Updated : 22 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The Ds\Vector::filter() function is used to filter out the only elements which satisfy the condition defined in the callback function. After doing a filter on the vector, it will eliminate the elements which do not satisfy the condition mentioned in the function. Syntax: Ds\Vector public Ds\Vector::filter( $callback ) Parameters: This function accepts single parameter $callback which returns true if the element in the vector is to be included, else return false. Return Value: This function returns the vector with all the elements which are filtered according to the callable function. Below programs illustrate the Ds\Vector::filter() function in PHP: Program 1: PHP <?php // Create new vector element $vector = new \Ds\Vector([1, 2, 3, 4, 5]); echo "Original Vector elements\n"; // Display the vector element var_dump($vector); echo("\nElements greater than or equal to 4\n"); // Filter the vector element var_dump($vector->filter(function($value) { return $value >= 4; })); ?> Output: Original Vector elements object(Ds\Vector)#1 (5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } Elements greater than or equal to 4 object(Ds\Vector)#3 (2) { [0]=> int(4) [1]=> int(5) } Program 2: PHP <?php // Create new vector element $vector = new \Ds\Vector([1, 2, 3, 4, 5]); echo "Original Vector elements\n"; // Display the vector element var_dump($vector); echo("\nOdd elements\n"); // Use filter() function to filter // the vector element var_dump($vector->filter(function($value) { return $value % 2 == 1; })); ?> Output: Original Vector elements object(Ds\Vector)#1 (5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } Odd elements object(Ds\Vector)#3 (3) { [0]=> int(1) [1]=> int(3) [2]=> int(5) } Reference: http://php.net/manual/en/ds-vector.filter.php Comment More infoAdvertise with us Next Article PHP | DsSequence filter() Function B barykrg Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_vector Similar Reads PHP | DsMap filter() Function The Ds\Map::filter() function is an inbuilt function in PHP which is used to create a new map using the filter function. Syntax: public Ds\Map::filter( $callback ) Parameters: It contains a single parameter $callback which is an optional parameter and it returns True if the value should be included, 2 min read PHP | DsSet filter() Function The Ds\Set::filter() function is an inbuilt function in PHP which is used to create new set using filter function. Syntax: Ds\Set public Ds\Set::filter( $callback ) Parameters: This function accepts single parameter $callback which is optional and it returns True if the value should be included, Fal 1 min read PHP | DsDeque 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 2 min read PHP | preg_filter() Function The preg_filter() function is an inbuilt function in PHP which is used to perform a regular expression search and replace the text. Syntax: preg_filter( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mention above and describe below. $pattern: 2 min read PHP | DsVector filter() Function The Ds\Vector::filter() function is used to filter out the only elements which satisfy the condition defined in the callback function. After doing a filter on the vector, it will eliminate the elements which do not satisfy the condition mentioned in the function. Syntax: Ds\Vector public Ds\Vector:: 2 min read PHP | DsSequence 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, F 1 min read PHP | Ds\Vector filter() Function min read Like