简体   繁体   English

使用变量变量作为数组前缀

[英]Use Variable Variable as Array Prefix

Is it possible to use a variable variable as an array prefix? 是否可以将变量变量用作数组前缀? I have a set of arrays with the format $x_settings , and I want to output the values of just one depending on which prefix matches a condition. 我有一组格式$x_settings的数组,并且我想根据匹配条件的前缀输出一个值。

This is an extremely stripped-down version of much more complex code, so thanks for your indulgence: 这是更复杂的代码的精简版本,因此感谢您的放纵:

$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;
  }
}

As you can see I tried using ${$env}_settings[] but that gave me a PHP error: 如您所见,我尝试使用${$env}_settings[]但这给了我一个PHP错误:

unexpected '_settings' (T_STRING) 意外的“ _settings”(T_STRING)

Possible? 可能?

Yes, it is possible. 对的,这是可能的。 Your loop should look like below: 您的循环应如下所示:

foreach( $environments as $env )
{
  if( $current_env == $env )
  {
    define('DEBUG', ${$env.'_settings'}['debug']);
    define('CAKE',  ${$env.'_settings'}['cake']);
    define('DEATH', ${$env.'_settings'}['death']);
    break;
  }
}

Notes: 笔记:

  • I've fixed the typo in your array declaration. 我已经修复了数组声明中的错字。 You were using just = instead of => . 您使用的是Just =而不是=>
  • I've added a break inside your loop - otherwise, you'll be trying to re-declare constants and that will cause PHP to output errors 我在循环中添加了一个break -否则,您将尝试重新声明常量,这将导致PHP输出错误
  • I've changed = to == . 我将=更改为== = is the assignment operator. =是赋值运算符。 You need to use == (loose comparison) or === (strict comparison) instead. 您需要使用== (宽松比较)或=== (严格比较)。

Demo 演示

Use 2D array for this purpose: 为此使用2D数组:

$current_env = 'local';

$environment_settings = array(
    'local' => array('debug' = TRUE, 'cake'  = TRUE, 'death' = FALSE),
    'dev' => array('debug' = TRUE, 'cake'  = FALSE, 'death' = FALSE),
    'prod' => array('debug' = TRUE, 'cake'  = TRUE, 'death' = FALSE)
);

if (isset($environment_settings[$current_env])) {
    foreach ($environment_settings[$current_env] as $name => $val)
        define(strtoupper($name), $value);
}

Why not just make a 2-d array... 为什么不制作二维数组呢?

$settings=array(
    "local" => array(
         'cake'=>TRUE,
         'death'=>FALSE
    ),
    "dev" =>array(...etc ...),
    "prod"=>array(...etc ...)
);

then: 然后:

if( $current_env = $env )
{
   define('DEBUG', $settings[$env]['debug']);
   define('CAKE', $settings[$env]['cake']);
   define('DEATH', $settings[$env]['death']);
}

(I just typed this in - there may be typos!) (我刚输入的内容-可能有错别字!)

It should be 它应该是

$local_settings = array
(
    'debug' => TRUE,
    'cake'  =>TRUE,
    'death' => FALSE
);

Make use of the => operator instead of = operator when assigning keys to values 将键分配给值时,请使用=>运算符而不是=运算符

I changed the values to strings just for testing. 我将值更改为仅用于测试的字符串。 Try this: 尝试这个:

$env = 'local';

$local_settings = array
(
  'debug' => 'TRUE',
  'cake'  => 'TRUE',
  'death' => 'FALSE'
);

$setting_selector=$env.'_settings';
echo ${$setting_selector}['debug'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM