check

Definition

1
Arr::check(array $array, mixed|callable $condition, int $flag = 0): bool
Copied!

Description

Check if some or every array element meets specified condition.
If CHECK_SOME flag is NOT present then every array element must meet specified condition in order to pass check.

Condition

$condition type
Description
callable
Callable should return truthy or falsy value (while using CHECK_STRICT flag, return values other than true are treated as false).
Callable is supplied with either only element value (function($value)) or pair of element value and key (function($value, $key)) as arguments. Arguments amount depend on callable definition and is dynamically resolved using reflection (defaults to 2 - value and key)
mixed
If condition type is different than callable then every array element is compared against it value.
$value == $condition by default
$value === $conditionif CHECK_STRICT flag is enabled

Flags

Can be used as stand alone (i.e.Arr::CHECK_STRICT) as well as in conjunction (i.e. Arr::CHECK_STRICT | Arr::CHECK_SOME)
$flag argument used to be a boolean parameter called $strict
But do not worry, it it is fully backward compatible due to in-flight type conversion from bool to int
Constant name
Description
CHECK_STRICT
In case condition is callable check if it result is exactly true
If condition is not callable, then check if array element is equal to it both by value and type
See Condition section for more info
CHECK_SOME
Check will return true on first array element that match specified condition or false if none of them matches it.
By default check method will return true only if ALL of array elements meet specified condition

Examples

1
$array = [1, '1', true];
2
3
// Every array element is EQUAL to 1 ($value == '1')
4
Arr::check($array, '1') -> true
5
6
// Only one array element is the SAME as 1 ($value === '1') which is not sufficient
7
Arr::check($array, '1', Arr::CHECK_STRICT) -> false
8
9
// When CHECK_SOME flag is present, one element is sufficient to pass check
10
Arr::check($array, '1', Arr::CHECK_STRICT | Arr::CHECK_SOME) -> true
11
12
// You can also use built-in functions to validate whole array
13
Arr::check($array, 'is_int') -> false
14
Arr::check($array, 'is_string') -> false
15
16
// When CHECK_SOME flag is present only one element need to meet specified condition
17
Arr::check($array, 'is_int', Arr::CHECK_SOME) -> true
18
Arr::check($array, 'is_string', Arr::CHECK_SOME) -> true
19
20
// Every value of array is truthy
21
Arr::check($array, function ($value) { return $value; }) -> true
22
// Above check can be simplified to
23
Arr::check($array, true) -> true
24
// Or even shorter
25
Arr::check($array, 1) -> true
26
27
// When CHECK_STRICT flag is present, check will fail for callback return values other true
28
Arr::check($array, function ($value) { return $value; }, Arr::CHECK_STRICT) -> false
29
// But when callback is written as follows check will pass
30
Arr::check($array, function ($value) { return boolval($value); }, Arr::CHECK_STRICT) -> true
31
// Above check will pass if we add CHECK_SOME flag cause of of the elements is exactly true
32
Arr::check($array, function ($value) { return $value; }, Arr::CHECK_STRICT | Arr::CHECK_SOME) -> true
33
34
35
// Callback function arguments count is automatically detected
36
Arr::check($array, function ($value, $key) { return $value > $key; }) -> false
37
// First array element 1 is greater than its key 0
38
Arr::check($array, function ($value, $key) { return $value > $key; }, Arr::CHECK_SOME) -> true
Copied!
Last modified 1yr ago
Copy link