You are reading the documentation for version 2.2. Magento 2.3 is the latest version available. Ensure you are reading the documentation for your version of Magento.

Array Manager

Overview

The Magento\Framework\Stdlib\ArrayManager library provides the ability to manage deeply nested associative arrays. The library is primarily used to handle data from UI components within DataProviders and Modifiers, which are actually part of a complicated process of parsing XML files in associative arrays.

Usage

Method Description
exists Checks if the node exists in a given associative array
get Returns the value of the key (or node) at the end of the path, null is returned if the node hasn’t been found.
move Moves a value from one path to another
remove Removes node and returns modified array
replace Updates the existing nodes and returns the modified array
set Set value into node and returns modified data

Example 1

The following example shows how to add a custom field to the checkout billing address using the LayoutProcessor implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/**
 * Process js Layout of block
 *
 * @param array $jsLayout
 *
 * @return array
 */
public function process($jsLayout)
{
    ...

    if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
        ['children']['shippingAddress']['children']['shipping-address-fieldset']['children'])
    ) {
        $fields = $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
        ['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];

        ...
    }

    ...
}

For a cleaner implementation of the previous example, use the Magento\Framework\Stdlib\ArrayManager, library to eliminate duplicate checking and get the required array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

use Magento\Framework\Stdlib\ArrayManager;

...
    /**
     * @var ArrayManager
     */
    private $arrayManager;

    /**
     * SomeClass constructor.
     *
     * @param ArrayManager $arrayManager
     */
    public function __construct(ArrayManager $arrayManager)
    {
        $this->arrayManager = $arrayManager;
    }

    /**
     * Process js Layout of block
     *
     * @param array $jsLayout
     *
     * @return array
     */
    public function process($jsLayout): array
    {
        $path = 'components/checkout/children/steps/children/shipping-step/children/shippingAddress/children/shipping-address-fieldset/children';

        if ($fields = $this->arrayManager->get($path, $jsLayout)) {
            ...
        }

        ...
    }

...

Example 2

Suppose you have the following nested array:

1
2
3
4
5
6
7
8
9
10
11
12
13
$data = [
    'response' => [
        'status' => 'OK',
        'result' => [
            'items' => [
                0 => 'First item',
                1 => 'Second item',
                ...
            ],
            ...
        ]
    ]
]

You can use the Magento\Framework\Stdlib\ArrayManager library to access items in the array:

1
2
3
4
5
6
7
8
9
10
11
...

if ($this->arrayManager->get('response/status', $data) === 'OK') {
    $items = $this->arrayManager->get('response/result/items', $data) ?? [];

    foreach ($items as $item) {
        ...
    }
}

...