# Iterating

## each

#### Definition

```php
Arr::each(array|Iterator|IteratorAggregate $iterable, callable $callback, int $mode = self::EACH_VALUE): array|Iterator|IteratorAggregate
```

#### Description

Traverse through array or iterable object and call callback for each element (ignoring the result).

#### Modes

| Constant name            | Description                                                                                                                                                |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| EACH\_VALUE              | Iterate using callback in form of `function($value)`                                                                                                       |
| EACH\_KEY\_VALUE         | Iterate using callback in form of `function($key, $value)`                                                                                                 |
| EACH\_VALUE\_KEY         | Iterate using callback in form of `function($value, $key)`                                                                                                 |
| EACH\_VALUE\_KEYS\_LIST  | <p>Iterate using callback in form of <code>function($value, $key1, $key2, ...)</code><br></p><p><strong>Only for array</strong> <code>$iterable</code></p> |
| EACH\_KEYS\_ARRAY\_VALUE | <p>Iterate using callback in form of <code>function(array $keys, $value)</code></p><p><br><strong>Only for array</strong> <code>$iterable</code></p>       |

#### Examples

```php
$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);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://minwork.gitbook.io/array/traversing-array/iterating.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
