|
| 1 | +--- |
| 2 | +group: php-developer-guide |
| 3 | +title: Array Manager |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The [`Magento\Framework\Stdlib\ArrayManager`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Stdlib/ArrayManager.php){:target="_blank"} library provides the ability to manage deeply nested associative arrays. |
| 9 | +The library is primarily used to handle data from UI components within [DataProviders]({{ page.baseurl }}/ui_comp_guide/concepts/ui_comp_data_source.html) and [Modifiers]({{ page.baseurl }}/ui_comp_guide/concepts/ui_comp_modifier_concept.html), which are actually part of a complicated process of parsing XML files in associative arrays. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +|Method|Description| |
| 14 | +|--- |--- | |
| 15 | +| `exists` | Checks if the node exists in a given associative array | |
| 16 | +| `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. | |
| 17 | +| `move` | Moves a value from one path to another | |
| 18 | +| `remove` | Removes node and returns modified array | |
| 19 | +| `replace` | Updates the existing nodes and returns the modified array | |
| 20 | +| `set` | Set value into node and returns modified data | |
| 21 | + |
| 22 | +#### Example 1 |
| 23 | + |
| 24 | +The following example shows how to add a custom field to the checkout billing address using the [LayoutProcessor implementation]({{ site.mage2bloburl }}/1f9186c3b9a96c5e642fd5d3d31ac5c7e1877d2b/app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php#L143){:target="_blank"}. |
| 25 | + |
| 26 | +```php |
| 27 | +<?php |
| 28 | +/** |
| 29 | + * Process js Layout of block |
| 30 | + * |
| 31 | + * @param array $jsLayout |
| 32 | + * |
| 33 | + * @return array |
| 34 | + */ |
| 35 | +public function process($jsLayout) |
| 36 | +{ |
| 37 | + ... |
| 38 | + |
| 39 | + if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] |
| 40 | + ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']) |
| 41 | + ) { |
| 42 | + $fields = $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step'] |
| 43 | + ['children']['shippingAddress']['children']['shipping-address-fieldset']['children']; |
| 44 | + |
| 45 | + ... |
| 46 | + } |
| 47 | + |
| 48 | + ... |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +For a cleaner implementation of the previous example, use the `Magento\Framework\Stdlib\ArrayManager`, library to eliminate duplicate checking and get the required array. |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +use Magento\Framework\Stdlib\ArrayManager; |
| 58 | + |
| 59 | +... |
| 60 | + /** |
| 61 | + * @var ArrayManager |
| 62 | + */ |
| 63 | + private $arrayManager; |
| 64 | + |
| 65 | + /** |
| 66 | + * SomeClass constructor. |
| 67 | + * |
| 68 | + * @param ArrayManager $arrayManager |
| 69 | + */ |
| 70 | + public function __construct(ArrayManager $arrayManager) |
| 71 | + { |
| 72 | + $this->arrayManager = $arrayManager; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Process js Layout of block |
| 77 | + * |
| 78 | + * @param array $jsLayout |
| 79 | + * |
| 80 | + * @return array |
| 81 | + */ |
| 82 | + public function process($jsLayout): array |
| 83 | + { |
| 84 | + $path = 'components/checkout/children/steps/children/shipping-step/children/shippingAddress/children/shipping-address-fieldset/children'; |
| 85 | + |
| 86 | + if ($fields = $this->arrayManager->get($path, $jsLayout)) { |
| 87 | + ... |
| 88 | + } |
| 89 | + |
| 90 | + ... |
| 91 | + } |
| 92 | + |
| 93 | +... |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +#### Example 2 |
| 98 | + |
| 99 | +Suppose you have the following nested array: |
| 100 | + |
| 101 | +```php |
| 102 | +$data = [ |
| 103 | + 'response' => [ |
| 104 | + 'status' => 'OK', |
| 105 | + 'result' => [ |
| 106 | + 'items' => [ |
| 107 | + 0 => 'First item', |
| 108 | + 1 => 'Second item', |
| 109 | + ... |
| 110 | + ], |
| 111 | + ... |
| 112 | + ] |
| 113 | + ] |
| 114 | +] |
| 115 | +``` |
| 116 | + |
| 117 | +You can use the `Magento\Framework\Stdlib\ArrayManager` library to access items in the array: |
| 118 | + |
| 119 | +```php |
| 120 | +... |
| 121 | + |
| 122 | +if ($this->arrayManager->get('response/status', $data) === 'OK') { |
| 123 | + $items = $this->arrayManager->get('response/result/items', $data) ?? []; |
| 124 | + |
| 125 | + foreach ($items as $item) { |
| 126 | + ... |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +... |
| 131 | +``` |
0 commit comments