Minwork Array
  • Minwork Array
  • Common methods
    • has
    • get → getNestedElement
    • set → setNestedElement
    • remove
    • clone
    • getKeysArray
  • Object oriented methods
    • General information
  • Traversing array
    • Finding
    • Iterating
  • Manipulating array
    • Mapping
    • Filtering
    • Grouping
    • Sorting
    • Computations
    • Flattening
  • Validating array
    • check
    • isEmpty
    • isNested
    • isArrayOfArrays
    • isAssoc
    • isUnique
    • isNumeric
    • hasKeys
  • Utility methods
    • pack
    • unpack
    • createMulti
    • forceArray
    • getDepth
    • random
    • shuffle
    • nth
    • getFirstKey
    • getLastKey
    • getFirstValue
    • getLastValue
Powered by GitBook
On this page

Was this helpful?

  1. Traversing array

Iterating

each

Definition

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

Iterate using callback in form of function($value, $key1, $key2, ...)

Only for array $iterable

EACH_KEYS_ARRAY_VALUE

Iterate using callback in form of function(array $keys, $value)

Only for array $iterable

Examples

$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);
PreviousFindingNextMapping

Last updated 5 years ago

Was this helpful?