是否可以使用变量变量作为数组前缀?我有一组格式为 的数组$x_settings
,我想根据哪个前缀与条件匹配来输出一个数组的值。
这是更复杂代码的极其精简的版本,因此感谢您的放纵:
$current_env = 'local';
$local_settings = array
(
'debug' => TRUE,
'cake' => TRUE,
'death' => FALSE
);
$environments = array
(
'local',
'dev',
'prod'
);
foreach( $environments as $env )
{
if( $current_env == $env )
{
define('DEBUG', ${$env}_settings['debug']);
define('CAKE', ${$env}_settings['cake']);
define('DEATH', ${$env}_settings['death']);
break;
}
}
如您所见,我尝试使用${$env}_settings[]
,但这给了我一个 PHP 错误:
意外的“_settings”(T_STRING)
可能的?