ishworkh/multi-level-array-iterator

Provides a way to loop through nested arrays with any depth
8,902
Install
composer require ishworkh/multi-level-array-iterator
Latest Version:v2.0.0
PHP:>=7.0
License:MIT
Last Updated:Feb 18, 2017
Links: GitHub  ·  Packagist
Maintainer: Ishworkh

(PHP) Multi Level Array Iterator

There are times when there is a need to iterate over an array being unknown about the level of nested array it contains. Multi Level Array Iterator lets u do that. This functionality supports associative array and is properly tested.

Basic Usage

$array = [
    1,
    2,
    [
        3,
        4
    ],
    [
        5,
        6,
        [
            7,
            8
        ]
    ],
    [
        9,
        [
            [
                10
            ]
        ],
        11
    ]
];

foreach(\ArrayIterator\ArrayIteratorFacade::iterate($array) as $key => $ArrayItem)
{
    echo $key . ' - ' . $ArrayItem->getValue() . "\n";
}

Result:
    0 - 1
    1 - 2
    0 - 3
    1 - 4
    0 - 5
    1 - 6
    0 - 7
    1 - 8
    0 - 9
    0 - 10
    2 - 11

\ArrayIterator\ArrayIteratorFacade::iterate($array) returns a generator of ArrayElementInterface.

ArrayElementInterface provides

  1. getValue():mixed
  2. getKeyHierarchy():KeyHierarchyInterface
    • getParentKey(int $parentLevel): string
    • getHierarchyLevel(): int

New instance of \ArrayIterator\Iterator\ArrayIterator can be created with \ArrayIterator\ArrayIteratorFactory which is accessible through \ArrayIterator\ArrayIteratorLocator. Both the facade or locator way can be used to get an instance of ArrayIterator.

It includes special support for laravel framework. For its integration with Laravel's dependency injection container \ArrayIterator\ArrayIteratorServiceProvider is available which can be included in the lists of service providers in config/app.php. After this, type hinting \ArrayIterator\ArrayIteratorFactory anywhere inside laravel app will resolve into \ArrayIterator\ArrayIteratorFactory properly.

Note: - For debugging purpose, echoing KeyHierarchyInterface prints out string representation of keys hierarchy starting from root array separated by '-'. - Upgraded to php7

Related Packages

seffeng/arr-helper

PHP extension array helper

2,080 0
antaresbr/arrayhandler

A custom PHP array handler based on Laravel's Illuminate/Support/Arr class

6,581 0
collector/collector

A collection library inspired by Laravel Collections, built using generators to...

208 15
aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar...

512,340 4,327
ginq/ginq

LINQ to Object inspired DSL for PHP

260,851 190

Version History

Version Released PHP License
v2.0.0 >=7.0 MIT
v1.3.0 >=5.5 MIT
v1.2.1 >=5.5 MIT
v1.2.0 ^5.5 || ^7.0 MIT
1.1.0