0

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

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

我的问题是我想要一个名为的数组,该数组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

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

谢谢

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

4

2 回答 2

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;
    }
于 2012-08-25T07:16:22.710 回答
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;
}
于 2012-08-25T07:36:22.243 回答