以递归方式重命名数组键

时间:2012-08-25 06:50:46

标签: php arrays

我一直试图自己解决这个问题,但找不到我能理解的解决方案。

我有一个通过读取动态配置文件加载我的应用程序时动态创建的数组。

我的问题是我想要一个名为alias的数组,它将被拾取并重命名['@attr']所在的父数组。这是一个显示我的意思的例子。

[database] => Array
        (
            [0] => Array // Would be renamed to "production"
                (
                    [host] => test
                    [user] => test
                    [pass] => test
                    [name] => test
                    [port] => test
                    [@attr] => Array
                        (
                            [alias] => production
                            [name] => live
                        )

                )

            [1] => Array // Would be renamed to "development"
                (
                    [user_data] => Array
                        (
                            [0] => Array // Would be renamed to "three"
                                (
                                    [user] => sample
                                    [@attr] => Array
                                        (
                                            [alias] => three
                                        )

                                )

                            [1] => Array // Would be renamed to "two"
                                (
                                    [user] => sample
                                    [@attr] => Array
                                        (
                                            [alias] => two
                                        )

                                )

                        )

                    [host] => test
                    [user] => test
                    [pass] => test
                    [name] => test
                    [port] => test
                    [@attr] => Array
                        (
                            [alias] => development
                        )

                )

        )

这是我目前的代码

private function _applyXmlAlias($array)
        {
            foreach ($array as $config)
            {
                if (is_array($config))
                {
                    if (isset($config['@attr']['alias']))
                    {
                        $alias = $config['@attr']['alias'];

                        unset($config['@attr']['alias']);

                        if (empty($config['@attr']))
                        {
                            unset($config['@attr']);
                        }

                        $this->_alias[$alias] = $config;

                        break;

                        //$this->_temp = reset();


                    } else {

                        $this->_applyXmlAlias($config);
                    }
                }
            }
        }

如果我执行_alias属性的print_r,我会得到这个

   Array
(
    [production] => Array
        (
            [host] => test
            [user] => test
            [pass] => test
            [name] => test
            [port] => 3306
            [@attr] => Array
                (
                    [name] => live
                )

        )

    [development] => Array
        (
            [user_data] => Array
                (
                    [0] => Array
                        (
                            [user] => sample
                            [@attr] => Array
                                (
                                    [alias] => three
                                )

                        )

                    [1] => Array
                        (
                            [user] => sample
                            [@attr] => Array
                                (
                                    [alias] => two
                                )

                        )

                )

            [host] => test
            [user] => test
            [pass] => test
            [name] => test
            [port] => 3306
        )

)

注意:我不确定为什么我的循环只使用键alias捕获最后一个嵌套数组,而不是其他具有值productiondevelopment

关于如何处理这个问题的任何想法?我觉得我很接近,只需要在正确的道路上找一点指南。

感谢

编辑:更新了我的功能以反映当前的尝试

2 个答案:

答案 0 :(得分:0)

您的算法正在进行深度优先搜索。问题是它不知道何时终止。

而不是void返回类型,尝试这样的事情:

    private function _applyXmlAlias($array)
    {
        $found_alias = FALSE;
        foreach ($array as $config)
        {
            if (is_array($config))
            {
                if (isset($config['@attr']['alias']))
                {
                    $this->_alias = reset(array_values($config));
                    $found_alias = TRUE;
                       break;
                } else {
                    if ($this->_applyXmlAlias($config)) {
                       $found_alias = TRUE;
                       break;
                    }
                }
            }
        }
        return $found_alias;
    }

答案 1 :(得分:0)

很可能不是最优雅的解决方案,但有效。

function renameArr($arr) {
    foreach($arr as $key=>$val) {
        if(is_array($val)) {
            if(isset($val['@attr']['alias'])) {
                unset($arr[$key]);
                $key = $val['@attr']['alias'];
            }
            $arr[$key] = renameArr($val);
        }
    }
    return $arr;
}