Arr::each(array|Iterator|IteratorAggregate $iterable, callable $callback, int $mode = self::EACH_VALUE): array|Iterator|IteratorAggregate
Traverse through array or iterable object and call callback for each element (ignoring the result).
$array = [
1 => [
2 => 'a',
3 => 'b',
4 => [
5 => 'c',
],
],
'test' => 'd',
];
// Value only - using default EACH_VALUE mode
Arr::each($array, function ($value) {
print_r($value);
// [ 2 => 'a', ...]
// 'd'
});
// Key, Value
Arr::each($array, function ($key, $value) {
echo "{$key}: \t\t";
print_r($value);
// 1: [2 => 'a', ...]
// test: 'd'
}, Arr::EACH_KEY_VALUE);
// Value, Key
Arr::each($array, function ($value, $key) {
echo "{$key}: \t\t";
print_r($value);
// 1: [2 => 'a', ...]
// test: 'd'
}, Arr::EACH_VALUE_KEY);
// Value, Keys list
Arr::each($array, function ($value, ...$keys) {
echo implode('.', $keys) . ': \t\t';
print_r($value);
// 1.2: 'a'
// 1.3: 'b'
// 1.4.5: 'c'
// test: 'd'
}, Arr::EACH_VALUE_KEYS_LIST);
// Keys array, value
Arr::each($array, function (array $keys, $value) {
echo implode('.', $keys) . ': \t\t';
print_r($value);
// 1.2: 'a'
// 1.3: 'b'
// 1.4.5: 'c'
// test: 'd'
}, Arr::EACH_KEYS_ARRAY_VALUE);