Filter
Contains methods that find values or keys.
Common properties of these methods:
- They check if $this satisfies a callback in some way.
- They don't modify $this.
- They return either bool, TKey, TValue or a subset of $this.
Table of Contents
Methods
- all() : bool
- Checks if all elements satisfy a callback function.
- any() : bool
- Checks if at least one element satisfies a callback function.
- filter() : static
- Filters elements using a callback function.
- find() : TValue|null
- Returns the first element satisfying a callback function.
- findKey() : TKey|null
- Returns the key of the first element satisfying a callback function.
Methods
all()
Checks if all elements satisfy a callback function.
public
all(callable(TValue $value, TKey $key): bool $callback) : bool
This method returns true if the given callback returns true for all elements. Otherwise, the method returns false.
Parameters
- $callback : callable(TValue $value, TKey $key): bool
-
The callback function to call to check each element. If callback returns false, false is returned from all() and callback will not be called for further elements.
Return values
bool —Returns true if callback returns true for all elements. Otherwise, returns false.
any()
Checks if at least one element satisfies a callback function.
public
any(callable(TValue $value, TKey $key): bool $callback) : bool
This method returns true if the given callback returns true for any element. Otherwise, the method returns false.
Parameters
- $callback : callable(TValue $value, TKey $key): bool
-
The callback function to call to check each element. If this function returns true, true is returned from any() and the callback will not be called for further elements.
Return values
bool —Returns true if there is at least one element for which callback returns true. Otherwise, the method returns false.
filter()
Filters elements using a callback function.
public
filter([null|mixed $callback = null ][, FilterMode $mode = FilterMode::UseValue ]) : static
Iterates over each element's value, passing them to the callback function. If the callback function returns true, the value is returned into the result.
Keys are preserved.
Parameters
- $callback : null|mixed = null
-
The callback function to use.
If no callback is supplied, all elements with empty values will be removed.
- $mode : FilterMode = FilterMode::UseValue
-
Flag determining what arguments are sent to callback:
Return values
staticfind()
Returns the first element satisfying a callback function.
public
find(callable(TValue $value, TKey $key): bool $callback) : TValue|null
This method returns the value of the first element for which the given callback returns true. If no matching element is found the function returns null.
Parameters
- $callback : callable(TValue $value, TKey $key): bool
-
The callback function to call to check each element.
If this function returns true, the value is returned from find() and the callback will not be called for further elements.
Return values
TValue|null —Returns the value of the first element for which the callback returns true. If no matching element is found null is returned.
findKey()
Returns the key of the first element satisfying a callback function.
public
findKey(callable(TValue $value, TKey $key): bool $callback) : TKey|null
This method returns the key of the first element for which the given callback returns true. If no matching element is found the function returns null.
Parameters
- $callback : callable(TValue $value, TKey $key): bool
-
The callback function to call to check each element.
If this function returns true, the key is returned from findKey() and the callback will not be called for further elements.
Return values
TKey|null —Returns the key of the first element for which the callback returns true. If no matching element is found null is returned.