PHP | Ds\Vector filter() Function Last Updated : 22 Aug, 2019 Comments Improve Suggest changes 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 | DsSet xor() Function The Ds\Set::xor() function is an inbuilt function in PHP which is used to create a new set which contains the value either in the first set or second set but not both. Syntax: Ds\Set public Ds\Set::xor ( Ds\Set $set ) Parameters: This function accepts a single parameter $set which is used to hold th 2 min read PHP | DsVector set() Function The Ds\Vector::set() function is an inbuilt function in PHP which is used to set the value in the vector at the given index. Syntax: void public Ds\Vector::set( $index, $value ) Parameters: This function accepts two parameters as mentioned above and described below: $index: This parameter holds the 2 min read PHP array_filter() Function This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback func 2 min read PHP | DsVector sort() Function The Ds\Vector::sort() function is an inbuilt function in PHP which is used to sort the elements of vector in-place. This will arrange the vector elements in increasing order. Syntax: void public Ds\Vector::sort( $comparator ) Parameters: This function accepts a single parameter $comparator which is 2 min read Like