1

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 2

1

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

function convert(&$input) {
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.