1

以以下配置为例:

array(
    'key' => 'value',
    'key2' => array(
        'key' => '@@INJECT@@',
        'key2' => 'value'
    ),
    'key3' => '@@INJECT@@'
);

然后将array其转换为Zend_Config对象

但是我尝试@@INJECT@@在转换 Zend_Config 对象后找到一种方法来替换为特定值。

使用 basic array,我array_walk_recursive()用来执行此任务,但我没有找到Zend_Configobject (而且我不想将Zend_Configobject 转换为array)。

4

1 回答 1

0

您可以滚动自己的递归walker,如下所示:

function configWalkRecursive(Zend_Config $config, $callback)
{
    foreach ($config as $key => $item) {
        if ($item instanceof Zend_Config) {
            // recurse into child Zend_Config objects
            configWalkRecursive($item, $callback);
        } else {
            // run the callback against the element
            $callback($key, $item, $config);
        }
    }
}

这遍历 Zend_Config 中的元素,如果找到另一个 Zend_Config 则递归,否则将当前项传递给回调函数。

然后,对于您的回调,您可以执行以下操作来测试项目并根据需要进行替换:

$array = array(
    'key' => 'value',
    'key2' => array(
        'key' => '@@INJECT@@',
        'key2' => 'value'
    ),
    'key3' => '@@INJECT@@'
);

$config = new Zend_Config($array, true);

print_r($config);

configWalkRecursive($config, function($key, $item, $configObject) {
    if ('@@INJECT@@' === $item) {
        $configObject->$key = 'somevalue';
    }
);

print_r($config);

输出如下所示:

Zend_Config Object
(
    [_allowModifications:protected] => 1
    [_index:protected] => 0
    [_count:protected] => 3
    [_data:protected] => Array
        (
            [key] => value
            [key2] => Zend_Config Object
                (
                    [_allowModifications:protected] => 1
                    [_index:protected] => 0
                    [_count:protected] => 2
                    [_data:protected] => Array
                        (
                            [key] => @@INJECT@@
                            [key2] => value
                        )

                    [_skipNextIteration:protected] => 
                    [_loadedSection:protected] => 
                    [_extends:protected] => Array
                        (
                        )

                    [_loadFileErrorStr:protected] => 
                )

            [key3] => @@INJECT@@
        )

    [_skipNextIteration:protected] => 
    [_loadedSection:protected] => 
    [_extends:protected] => Array
        (
        )

    [_loadFileErrorStr:protected] => 
)
Zend_Config Object
(
    [_allowModifications:protected] => 1
    [_index:protected] => 3
    [_count:protected] => 3
    [_data:protected] => Array
        (
            [key] => value
            [key2] => Zend_Config Object
                (
                    [_allowModifications:protected] => 1
                    [_index:protected] => 2
                    [_count:protected] => 2
                    [_data:protected] => Array
                        (
                            [key] => somevalue
                            [key2] => value
                        )

                    [_skipNextIteration:protected] => 
                    [_loadedSection:protected] => 
                    [_extends:protected] => Array
                        (
                        )

                    [_loadFileErrorStr:protected] => 
                )

            [key3] => somevalue
        )

    [_skipNextIteration:protected] => 
    [_loadedSection:protected] => 
    [_extends:protected] => Array
        (
        )

    [_loadFileErrorStr:protected] => 
)
于 2013-01-18T12:14:40.303 回答