Access an associative array value given an array of keys in PHP I have an object which stores an app configuration in an associative array, as this:

[
  'Database' =&gt; 
    [
      'DB_SERVER' =&gt; 'localhost'
      'DB_NAME' =&gt; 'adbname'
      'DB_USER' =&gt; 'theuser'
      'DB_PASS' =&gt; 'thepass'
    ]
]


It could have more than two levels.

And a function, get(), which receives a variable number of arguments and returns the configuration value for this parameters. For example, Config::get('Database','DB_PASS') will return 'thepass'.

This is an excerpt of the code:

class Config
{
    protected static $values;

    ...

    public static function get()
    {
        $val = &amp;self::$values;
        $argList = func_get_args();
        for ($i = 0; $i &lt; count($argList); $i++) {
            $val = &amp;$val[$argList[$i]];
            if(empty($val)) break;
        }
        return (is_null($val)?"":$val);
    }
}


Is there a more elegant / efficient way of accessing the value?

(note: I posted the question here but I was notified this web was a proper place to ask)
Your code seems fine, but it's a bit verbose. You can make it more elegant with a foreach loop, clean up the way you exit the function, and if you're using PHP 5.6 or above you can use a real variable-length argument list. See:

http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

class ConfigurationHandle
{
    protected static $configuration;

    &lt;...&gt;

    public static function get(...$arguments)
    {
        $value = &amp;self::$configuration;
        foreach($arguments as $argument)
        {  
          $value = &amp;$value[$argument];
          if(empty($value)) return "";
        }
        return $value;
    }
}


I've written out full words, because there is no need to abbreviate. I think this code is more elegant, because it is easier to read and understand, but it is not more efficient. 

It would be more efficient if you dumped the Config class altogether, and used a one dimensional array like this:

$configuration = ['DATABASE_SERVER' =&gt; 'localhost',
                  'DATABASE_NAME'   =&gt; 'adbname',
                  'DATABASE_USER'   =&gt; 'theuser',
                  'DATABASE_PASS'   =&gt; 'thepass'];


That is immeasurable more efficient, and might I say it's elegant too?

If top speed is not important to you, but multiple levels, and more control, are, then go for the class.