Filtering

filter

Definition

1
Arr::filter(array $array, ?callable $callback = null, int $flag = 0): array
Copied!

Description

Wrapper around PHP built-in array_filter method to allow chaining in ArrObj

Examples

filterByKeys

Definition

1
Arr::filterByKeys(array $array, mixed $keys, bool $exclude = false): array
Copied!

Description

Filter array values by preserving only those which keys are present in array obtained from $keys variable

Examples

1
$array = [
2
'a' => 1,
3
'b' => 2,
4
3 => 'c',
5
4 => 5
6
];
7
8
Arr::filterByKeys($array, 'a.b.3') -> ['a' => 1, 'b' => 2, 3 => 'c']
9
Arr::filterByKeys($array, 'a.b.3', true) -> [4 => 5]
10
11
Arr::filterByKeys($array, [null, 0, '']) -> []
12
Arr::filterByKeys($array, [null, 0, ''], true) -> $array
Copied!

filterObjects

Definition

1
Arr::filterObjects(array $objects, string $method, ...$args): array
Copied!

Description

Filter objects array using return value of specified method
This method also filter values other than objects by standard boolean comparison

Examples

1
$object = new class() {
2
function test($preserve = true) {
3
return $preserve;
4
}
5
};
6
7
$array = [$object, 'foo', $object, false];
8
9
Arr::filterObjects($array, 'test') -> [$object, 'foo', $object]
10
Arr::filterObjects($array, 'test', false) -> ['foo']
Copied!
Last modified 1yr ago