您可以滚动自己的递归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] =>
)