Method filter()
- Method
filter
mixedfilter(mixedarr,void|mixedfun,mixed...extra)- Description
Filters the elements in
arrthroughfun.- Parameter
arr arris treated as a set of elements to be filtered, as follows:arrayEach element is filtered with
fun. The return value is of the same type asarrand it contains the elements thatfunaccepted.funis applied in order to each element, and that order is retained between the kept elements.mappingThe values are filtered with
fun, and the index/value pairs it accepts are kept in the returned mapping.programThe program is treated as a mapping containing the identifiers that are indexable from it and their values.
objectIf there is a lfun::cast method in the object, it's called to try to cast the object to an array, a mapping, or a multiset, in that order, which is then filtered as described above.
- Parameter
fun Unless something else is mentioned above,
funis used as filter like this:arrayWhen both
arrandfunare arrays, they should have the same lengths. In this case, the elements inarrare kept where the corresponding positions infunare nonzero.function(mixed... :mixed)funis called for each element. It gets the current element as the first argument andextraas the rest. The element is kept iffunreturns true, otherwise it's filtered out.objectThe object is used as a
functionlike above, i.e. the lfun::`() method in it is called.multisetfunis indexed with each element. The element is kept if the result is nonzero, otherwise it's filtered out.zero|voidEach element that is callable is called with
extraas arguments. The element is kept if the result of the call is nonzero, otherwise it's filtered out. Elements that aren't callable are also filtered out.stringEach element is indexed with the given string. If the result of that is zero then the element is filtered out, otherwise the result is called with
extraas arguments. The element is kept if the return value is nonzero, otherwise it's filtered out.This is typically used when
arris a collection of objects, andfunis the name of some predicate function in them.- Note
The function is never destructive on
arr.- See also
map(),
foreach()