Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function to replace only some values in multidimensional array in PHP

I have a base set of rules stored in an array as follows:

$base_settings = [
    'ruleset1' => [
        'rule1' => ['param1' => 50, 'param2' => 100],
        'rule2' => ['param1' => 6, 'param2' => 3, 'param3' => 2],
        'rule3' => ['param1' => 1500, 'param2' => 300, 'param3' => 100],
        'rule4' => 2
    ],
    'ruleset2' => 'date',
    'ruleset3' => [
        'param1' => ['string1', 'string2', 'string3', 'string4'],
        'param2' => ['string1', 'string2']
    ]
];

I also have a user-specific array which contains values that need to override the values in the array above. For example:

$priority_setting = [
    'ruleset1' => [
        'rule2' => ['param2' => 4, 'param3' => 6]
    ]
];

The $priority_settings array can be any ‘subset’ of the $base_settings array allowing the updating of many different rulesets. I am looking for a function that overrides the specific parts of the base array with those of the user-specific array, but without affecting the other elements. The result for the two arrays above should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$final_settings = [
    'ruleset1' => [
        'rule1' => ['param1' => 50, 'param2' => 100],
        'rule2' => ['param1' => 6, 'param2' => 4, 'param3' => 6],
        'rule3' => ['param1' => 1500, 'param2' => 300, 'param3' => 100],
        'rule4' => 2
    ],
    'ruleset2' => 'date',
    'ruleset3' => [
        'param1' => ['string1', 'string2', 'string3', 'string4'],
        'param2' => ['string1', 'string2']
    ]
];

The built-in PHP function, array_merge_recursive nearly does the trick, but adds rather than replaces. I am having problems with the fact that there are different depths to the rulesets leading to problems such as the $priority_settings array above ‘deleting’ param1 of rule 2, rather than just replacing param2 and param3 in the $final_settings array. Any help would be very much appreciated!

>Solution :

You are very close!

To add key/values into an array and replace key/values if they already exist you are looking for array_replace_recursive()

This function will recurse down the second parameter array, replacing any existing values, and adding any key/value pairs that do not exist.

$base_settings = [
    'ruleset1' => [
        'rule2' => ['param1' => 1, 'param2' => 2, 'param3' => 3],
    ],
];

$priority_setting = [
    'ruleset1' => [
        'rule2' => ['param2' => 420, 'param3' => 69]
    ]
];

//Update base settings with new values from priority settings
$base_settings = array_replace_recursive($base_settings, $priority_setting);

This results in the new $base_settings array:

Array(
    [ruleset1] => Array(
        [rule2] => Array(
            [param1] => 1
            [param2] => 420
            [param3] => 69
        )
    )
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading