4

Is there any good example of array manipulation in magento 2 using Class Magento\Framework\Stdlib\ArrayManager and to use their function in my module like marge array, find any nodes etc.

I just wondering how to use in module.

7

This class is dedicated to handling/manipulating nested associative arrays. More specifically, heavily nested associative arrays. It is used primarily when handling data from UiComponents within DataProviders and Modifiers; which, are actually part of a complicated process of parsing XML files as associative arrays!

Most of the methods in the API abstract array traversal by representing them as "paths." (i.e. 'firstKey/secondKey/thirdKey' is equivalent to:

[firstKey] => [
    [secondKey] => [
        [thirdKey] => 'someValue'
    ]
]

where 'someValue' is the value associated with the last key in the "path")

Below is a description of each method in the API and an example of how Magento uses them in their core code:


Exists()

Checks if a path exits in a given associative array

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php#L607

Get()

This will retrieve the value of the key (or node) at the end of the path

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/ScheduleDesignUpdate.php#L108

Set()

Sets a value into a node, and returns the modified associative array

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Config/Model/Config/Processor/EnvironmentPlaceholder.php#L64

Replace()

Sets a value into existing nodes and returns the modified array

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php#L82

Move()

Moves a value from one path, to another. If the path doesn't exist, the method provides the option to populate that path with the $overwrite parameter

Example: https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Stdlib/Test/Unit/ArrayManagerTest.php#L207

Unfortunately, I couldn't find an example (outside the unit test) of this method being used in the core code :/

Merge()

This is an abstraction for the PHP method array_replace_recursive. It allows you to do the same thing as the aforementioned SPL method, but staring at specified path

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/GiftMessage/Ui/DataProvider/Product/Modifier/GiftMessage.php#L107

Populate()

Creates an index (or indexes), given a path. It will create an empty array at the end of the path

Example: https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Stdlib/Test/Unit/ArrayManagerTest.php#L307

Same story as Move(), couldn't find this being used anywhere in the code base...

Remove()

Removes (or "unsets") a particular value based on a given path

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php#L301

findPaths()

Returns an array filled with paths where a specified index exists

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php#L113

findPath()

Gets the first matching path for elements with specified indexes

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Downloadable/Ui/DataProvider/Product/Form/Modifier/UsedDefault.php#L144

slicePath()

An abstraction for array_slice that allows for retrieval/offsetting of a string sequence representing a path in an associative array

Example: https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/AdvancedPricing.php#L565

  • Thanks for sharing user information but how to change [thirdKey] => 'someValue' to [thirdKey] => 'someValueOtherValue' i try to use marge method but no lucky can u explain how to change values – Arshad Muhammad Mar 12 '18 at 9:36
  • Use replace() instead of merge for that scenario. – Ethan Yehuda Mar 12 '18 at 12:20
  • On the other hand, merge() should be able to replace keys that exist in the first array and the second array. I would use replace() if you want to change one value and merge() for changing multiple values. Can you show a snippit of your code using merge(), and an example of the data you're trying to replace/merge? – Ethan Yehuda Mar 12 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.