filter()
The filter() method creates a new array with all elements that pass the function.
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(range(1, 10));
//Filtering values between 3 and 6
$arrayUtils->filter(function($num){ return $num < 3 || $num > 6; });
// expected output: [1, 2, 7, 8, 9, 10]Syntax
$arrayUtils->filter(callable $callback) : ArrayUtils;Parameter
$callbackA function to test each element of the array. Return a value that coerces to
TRUEto keep the element, or toFALSEotherwise. Function taking three arguments:$valueThe current element being processed in the array.$keyThe index of the current element being processed in the array.$arrayThe arrayeverywas called upon.
Return value
A filtered array.
Prefixing
$arrayUtils->filterAs(callable $callback) : array;ArrayUtils::filterFrom(iterable $from, callable $callback) : ArrayUtils;ArrayUtils::filterFromAs(iterable $from, callable $callback) : array;References
Last updated