Take in example the following config:
array(
'key' => 'value',
'key2' => array(
'key' => '@@INJECT@@',
'key2' => 'value'
),
'key3' => '@@INJECT@@'
);
Then this array
is converted to Zend_Config
object
But I try to find a way to replace @@INJECT@@
with a specific value after converting Zend_Config object.
With a basic array
, I use array_walk_recursive()
to perform this task, but I do not find for Zend_Config
object (and I don't want to convert Zend_Config
object to array
).
You could roll your own recursive walker, something like this:
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);
}
}
}
This iterates over the elements in the Zend_Config, recurses if it finds another Zend_Config, otherwise passes the current item to a callback function.
Then, for your callback you could do something like this to test the items and replace as necessary:
$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);
The output looks like this:
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] =>
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.