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