Tùng Nguyễn
Tùng Nguyễn

Reputation: 103

Rename index key of multi-dimensional array php

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

Upvotes: 1

Views: 174

Answers (2)

Temanyl
Temanyl

Reputation: 46

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

Upvotes: 1

Kien Nguyen
Kien Nguyen

Reputation: 2701

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

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

Upvotes: 1

Related Questions