以下面的配置为例:
array(
'key' => 'value',
'key2' => array(
'key' => '@@INJECT@@',
'key2' => 'value'
),
'key3' => '@@INJECT@@'
);
然后此array
转换为Zend_Config
对象
但是我尝试在转换Zend_Config对象后找到一种用特定值替换@@INJECT@@
的方法。
使用基本array
,我使用array_walk_recursive()
执行此任务,但我找不到Zend_Config
对象(我不想转换Zend_Config
反对array
)。
答案 0 :(得分:0)
您可以滚动自己的递归步行器,如下所示:
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] =>
)