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
$callback
A function to test each element of the array. Return a value that coerces to
TRUE
to keep the element, or toFALSE
otherwise. Function taking three arguments:$value
The current element being processed in the array.$key
The index of the current element being processed in the array.$array
The arrayevery
was 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