|
| 1 | +# Iterating |
| 2 | + |
| 3 | +## each |
| 4 | + |
| 5 | +#### Definition |
| 6 | + |
| 7 | +```php |
| 8 | +Arr::each(array|Iterator|IteratorAggregate $iterable, callable $callback, int $mode = self::EACH_VALUE): array|Iterator|IteratorAggregate |
| 9 | +``` |
| 10 | + |
| 11 | +#### Description |
| 12 | + |
| 13 | +Traverse through array or iterable object and call callback for each element \(ignoring the result\). |
| 14 | + |
| 15 | +#### Modes |
| 16 | + |
| 17 | +<table> |
| 18 | + <thead> |
| 19 | + <tr> |
| 20 | + <th style="text-align:left">Constant name</th> |
| 21 | + <th style="text-align:left">Description</th> |
| 22 | + </tr> |
| 23 | + </thead> |
| 24 | + <tbody> |
| 25 | + <tr> |
| 26 | + <td style="text-align:left">EACH_VALUE</td> |
| 27 | + <td style="text-align:left">Iterate using callback in form of <code>function($value)</code> |
| 28 | + </td> |
| 29 | + </tr> |
| 30 | + <tr> |
| 31 | + <td style="text-align:left">EACH_KEY_VALUE</td> |
| 32 | + <td style="text-align:left">Iterate using callback in form of <code>function($key, $value)</code> |
| 33 | + </td> |
| 34 | + </tr> |
| 35 | + <tr> |
| 36 | + <td style="text-align:left">EACH_VALUE_KEY</td> |
| 37 | + <td style="text-align:left">Iterate using callback in form of <code>function($value, $key)</code> |
| 38 | + </td> |
| 39 | + </tr> |
| 40 | + <tr> |
| 41 | + <td style="text-align:left">EACH_VALUE_KEYS_LIST</td> |
| 42 | + <td style="text-align:left"> |
| 43 | + <p>Iterate using callback in form of <code>function($value, $key1, $key2, ...)</code> |
| 44 | + <br |
| 45 | + /> |
| 46 | + </p> |
| 47 | + <p><b>Only for array</b> <code>$iterable</code> |
| 48 | + </p> |
| 49 | + </td> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td style="text-align:left">EACH_KEYS_ARRAY_VALUE</td> |
| 53 | + <td style="text-align:left"> |
| 54 | + <p>Iterate using callback in form of <code>function(array $keys, $value)</code> |
| 55 | + </p> |
| 56 | + <p><b><br />Only for array</b> <code>$iterable</code> |
| 57 | + </p> |
| 58 | + </td> |
| 59 | + </tr> |
| 60 | + </tbody> |
| 61 | +</table>#### Examples |
| 62 | + |
| 63 | +```php |
| 64 | +$array = [ |
| 65 | + 1 => [ |
| 66 | + 2 => 'a', |
| 67 | + 3 => 'b', |
| 68 | + 4 => [ |
| 69 | + 5 => 'c', |
| 70 | + ], |
| 71 | + ], |
| 72 | + 'test' => 'd', |
| 73 | +]; |
| 74 | + |
| 75 | +// Value only - using default EACH_VALUE mode |
| 76 | +Arr::each($array, function ($value) { |
| 77 | + print_r($value); |
| 78 | + // [ 2 => 'a', ...] |
| 79 | + // 'd' |
| 80 | +}); |
| 81 | + |
| 82 | +// Key, Value |
| 83 | +Arr::each($array, function ($key, $value) { |
| 84 | + echo "{$key}: \t\t"; |
| 85 | + print_r($value); |
| 86 | + // 1: [2 => 'a', ...] |
| 87 | + // test: 'd' |
| 88 | +}, Arr::EACH_KEY_VALUE); |
| 89 | + |
| 90 | +// Value, Key |
| 91 | +Arr::each($array, function ($value, $key) { |
| 92 | + echo "{$key}: \t\t"; |
| 93 | + print_r($value); |
| 94 | + // 1: [2 => 'a', ...] |
| 95 | + // test: 'd' |
| 96 | +}, Arr::EACH_VALUE_KEY); |
| 97 | + |
| 98 | +// Value, Keys list |
| 99 | +Arr::each($array, function ($value, ...$keys) { |
| 100 | + echo implode('.', $keys) . ': \t\t'; |
| 101 | + print_r($value); |
| 102 | + // 1.2: 'a' |
| 103 | + // 1.3: 'b' |
| 104 | + // 1.4.5: 'c' |
| 105 | + // test: 'd' |
| 106 | +}, Arr::EACH_VALUE_KEYS_LIST); |
| 107 | + |
| 108 | + |
| 109 | +// Keys array, value |
| 110 | +Arr::each($array, function (array $keys, $value) { |
| 111 | + echo implode('.', $keys) . ': \t\t'; |
| 112 | + print_r($value); |
| 113 | + // 1.2: 'a' |
| 114 | + // 1.3: 'b' |
| 115 | + // 1.4.5: 'c' |
| 116 | + // test: 'd' |
| 117 | +}, Arr::EACH_KEYS_ARRAY_VALUE); |
| 118 | +``` |
| 119 | + |
0 commit comments