可能是這樣的代碼將幫助您
function _flattenArray($array, &$flatten, $index = null, $path = null)
{
if (\is_array($array)) {
foreach ($array as $k => $v) {
_flattenArray(
$v,
$flatten,
(isset($index) ? $index . '.' : '') . $k,
(isset($path) ? $path : '') . '[' . $k . ']'
);
}
} else {
$flatten[$index] = [
'path' => $path,
'value' => $array,
];
}
}
$config = ...; // load config
$flatten = [];
_flattenArray($config, $flatten);
$parameterBag = [];
foreach ($flatten as $k => $v) {
$parameterBag[$k] = $v['value'];
}
$parameterBag = new \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag($parameterBag);
$parameterBag->resolve();
$parameterBag = $parameterBag->all();
$propertyAccessor = \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor();
foreach ($parameterBag as $k => $v) {
if ($v === $flatten[$k]['value']) continue;
$propertyAccessor->setValue($config, $flatten[$k]['path'], $v);
}
dump($config);
據我所知Symfony的配置組件無法處理數據的這種方式。嘗試在加載後進行後處理。 –
我已經對'import'和merge進行了後期處理!但我真的堅持**動態更換**! 我對處理數據有很多想法: - 將配置數組複製到簡單的一個級別數組,並在循環原始數組時使用它 - 循環並遞歸替換 - 剪切並提取'%%'參數及其位置並取而代之 我真的不知道該如何處理! – NITS
我認爲這是關於2-nd平面配置的好主意。試着看看DI組件如何「DependencyInjection \ ParameterBag \ ParameterBag」解析參數。或者找到現成的決定。我無法給出確切的建議。 –