Rename index key of multi-dimensional array php

Viewed 91

I have a multi-dimensional array below and I want to change all conditions key to new_child

Array
(
    [0] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
            [conditions] => Array
                (
                    [0] => Array
                        (
                            [type] => MagentoCatalogRuleModelRuleConditionCombine
                            [attribute] => category_ids
                            [conditions] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                                            [attribute] => category_ids
                                        )
                                    [1] => Array
                                        (
                                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                                            [attribute] => category_ids
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
            [conditions] => Array
                (
                    [0] => Array
                        (
                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                            [attribute] => category_ids
                        )
                )
        )
)

my expected result is like this:

Array
(
    [0] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
            [new_child] => Array          //change will be made here
                (
                    [0] => Array
                        (
                            [type] => MagentoCatalogRuleModelRuleConditionCombine
                            [attribute] => category_ids
                            [new_child] => Array          //change will be made here
                                (
                                    [0] => Array
                                        (
                                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                                            [attribute] => category_ids
                                        )
                                    [1] => Array
                                        (
                                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                                            [attribute] => category_ids
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [type] => MagentoCatalogRuleModelRuleConditionCombine
            [aggregator] => all
            [new_child] => Array               //change will be made here
                (
                    [0] => Array
                        (
                            [type] => MagentoCatalogRuleModelRuleConditionProduct
                            [attribute] => category_ids
                        )
                )
        )
)

I have tried with the recursion function and it not work. It only changes for parent array and still not change the key name in the child array.

Here is my code

function convert($input) {
    foreach ( $input as $k => &$v )
    {
        if (array_key_exists('conditions', $input[$k])) {
            $input[$k]['new_child'] = $input[$k]['conditions'];
            unset($input[$k]['conditions']);
            convert($input[$k]['new_child']);
        }
        
    }
    return $input;
}
print_r(convert($input));
2 Answers

Logic of your function is fine, but you need to pass $input parameter by reference:

function convert(&$input) {
   ...
}

The problem is, that you are not using the result of the recursive call of convert. You should be able to fix your code by doing this:

function convert($input) {
    foreach ( $input as $k => $v )
    {
        if (array_key_exists('conditions', $input[$k])) {
            $input[$k]['new_child'] = convert($input[$k]['conditions']);
            unset($input[$k]['conditions']);
        }
        
    }
    return $input;
}
Related