【问题标题】:Rename index key of multi-dimensional array php重命名多维数组php的索引键
【发布时间】:2021-05-25 08:35:46
【问题描述】:

我下面有一个多维数组,我想把所有conditions键改成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
                        )
                )
        )
)

我的预期结果是这样的:

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

我尝试了递归函数,但它不起作用。它只改变父数组,仍然不改变子数组中的键名。

这是我的代码

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

【问题讨论】:

    标签: php arrays recursion


    【解决方案1】:

    问题是,您没有使用 convert 递归调用的结果。 您应该能够通过这样做来修复您的代码:

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

    【讨论】:

      【解决方案2】:

      你的函数逻辑没问题,但你需要通过引用传递$input参数:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-09
        • 1970-01-01
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 2012-09-16
        相关资源
        最近更新 更多