Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

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:

unexpected '_settings' (T_STRING)

Possible?

share|improve this question
    
Why can't you just use 2D array? –  u_mulder Feb 7 '14 at 18:00
    
$current_env = $env is always true, as this means "set $current_env to the value of $env". Did you mean the conditional operators == or ===? –  h2ooooooo Feb 7 '14 at 18:06

5 Answers 5

up vote 3 down vote accepted

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 =>.
  • 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
  • I've changed = to ==. = is the assignment operator. You need to use == (loose comparison) or === (strict comparison) instead.

Demo

share|improve this answer

Use 2D array for this purpose:

$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);
}
share|improve this answer
    
great minds think alike :) –  TonyWilk Feb 7 '14 at 18:08

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!)

share|improve this answer

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

share|improve this answer

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'];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.