0

この配列を例にとると、見つかったキーの2レベル上のキーの名前を変更するにはどうすればよいですか。

[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']);
                        }

                        reset($config);

                        while (list($key, $val) = each($config))
                        {

                            $this->_config[$alias][$key] = $val;
                        }

                        //$this->_config[$alias] = $config;

                        //$this->_applyXmlAlias($this->_alias[$alias]);

                    } else {

                        $this->_applyXmlAlias($config);
                    }

                } else {

                    break;
                }
            }
        }

この配列出力を取得します

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

                )

            [1] => 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] => test
                    [@attr] => Array
                        (
                            [alias] => development
                        )

                )

        )

    [production] => Array
        (
            [host] => test
            [user] => test
            [pass] => test
            [name] => test
            [port] => test
            [@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] => test
        )

    [three] => Array
        (
            [user] => sample
        )

    [two] => Array
        (
            [user] => sample
        )

)

ご覧のとおり、出力は多少得られています。alias唯一の問題は、新しいアレイをマスター構成アレイの新しい名前に置き換える必要があることです。[@attr][alias]また、同じ親アレイに属し、マスター構成アレイに追加されないキーを含むすべての子アレイをアレイに通知する必要があります。

何か案は?

4

1 に答える 1

0

問題の説明から私が理解している限り、再帰的/スタックベースのソリューションを使用する必要があります。しかし、それはコストがかかるだけでなく、複雑になります。これを行う必要があるシナリオを説明できれば、議論によってより良い設計が可能になるでしょう :)

于 2012-08-29T09:06:33.823 に答える