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'
    ]
]


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?
This seems alright, but you could use foreach, and checking and returning can be simpler:

class Config
{
    protected static $values;

    ...

    public static function get()
    {
        $val = &amp;self::$values;
        foreach(func_get_args() as $argument)
        {  
          $val = &amp;$val[$argument];
          if(empty($val)) return "";
        }
        return $val;
    }
}


I would call this more elegant, but it's not more efficient. (I did not test the code.)