Edit in GitHubLog an issue

Array manager

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#

MethodDescription
existsChecks if the node exists in a given associative array
findFinds a node in a nested array and saves its index and parent node reference
findPathsGets matching paths for elements with specified indexes.
getReturns the value of the key (or node) at the end of the path. null is returned if the node could not be found.
moveMoves a value from one path to another
mergeMerges a value with a node and returns the modified data.
populatePopulates a nested array, if possible and necessary.
removeRemoves a node and returns the modified array.
replaceUpdates the existing nodes and returns the modified array
setSets the value into a node and returns modified data.
slicePathRetrieves a slice of the specified path.

Example 1#

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

Copied to your clipboard
1<?php
2/**
3 * Process js Layout of block
4 *
5 * @param array $jsLayout
6 *
7 * @return array
8 */
9public function process($jsLayout)
10{
11 ...
12
13 if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
14 ['children']['shippingAddress']['children']['shipping-address-fieldset']['children'])
15 ) {
16 $fields = $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
17 ['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];
18
19 ...
20 }
21
22 ...
23}

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

Copied to your clipboard
1<?php
2
3use Magento\Framework\Stdlib\ArrayManager;
4
5...
6 /**
7 * @var ArrayManager
8 */
9 private $arrayManager;
10
11 /**
12 * SomeClass constructor.
13 *
14 * @param ArrayManager $arrayManager
15 */
16 public function __construct(ArrayManager $arrayManager)
17 {
18 $this->arrayManager = $arrayManager;
19 }
20
21 /**
22 * Process js Layout of block
23 *
24 * @param array $jsLayout
25 *
26 * @return array
27 */
28 public function process($jsLayout): array
29 {
30 $path = 'components/checkout/children/steps/children/shipping-step/children/shippingAddress/children/shipping-address-fieldset/children';
31
32 if ($fields = $this->arrayManager->get($path, $jsLayout)) {
33 ...
34 }
35
36 ...
37 }
38
39...
40

Example 2#

Suppose you have the following nested array:

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

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

Copied to your clipboard
1...
2
3if ($this->arrayManager->get('response/status', $data) === 'OK') {
4 $items = $this->arrayManager->get('response/result/items', $data) ?? [];
5
6 foreach ($items as $item) {
7 ...
8 }
9}
10
11...

You can use the Magento\Framework\Stdlib\ArrayManager library to populate an array from the given path:

Copied to your clipboard
1...
2
3$this->arrayManager->populate('response/result/items', $data)
4
5...
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.