是否有可能将变量计算为多维PHP数组索引

Given a multidimensional array or dictionary $array.

And assuming that $array['foo']['bar']['baz'] = 'something';

Is there a way other than via an eval statement for me use the multi-dimentional index foo/bar/baz? (The use case is in creating the index dynamically i.e. The function does not know what /foo/bar/baz/ is).

The only way I could figure to do this was:

$item = testGetIndex($array, "'foo']['bar']['baz'");

function testGetIndex($array, $index) {
  eval('$item = $array[' . $index . '];');
  return $item;
}

Note: I should mention that I do not want to search this array. This is a weird use case. I am being passed a very large multi dimensional array and it's ugly to have to use constructs like..

$array[foo][bar]..[baz] to make modifications to the array.

To modify an array using a path:

function setPath(&$root, $path, $value)
{
        $paths = explode('/', $path);
        $current = &$root;

        foreach ($paths as $path) {
                if (isset($current[$path])) {
                        $current = &$current[$path];
                } else {
                        return null;
                }
        }

        return $current = $value;
}
$path = 'foo/bar/baz';
$root = array('foo' => array('bar' => array('baz' => 'something')));

setPath($root, $path, '123');

You can tweak the function to just return a reference to the element you wish to change:

function &getPath(&$root, $path)
{
        $paths = explode('/', $path);
        $current = &$root;

        foreach ($paths as $path) {
                if (isset($current[$path])) {
                        $current = &$current[$path];
                } else {
                        return null;
                }
        }

        return $current;
}

$x = &getPath($root, $path);

$x = 456; // $root['foo']['bar']['baz'] == 456

Blatently reusing my answer here:

function recurseKeys(array $keys,array $array){
    $key = array_shift($keys);
    if(!isset($array[$key])) return null;
    return empty($keys) ?
        $array[$key]:
        recurseKeys($keys,$array[$key];
}
$values = recurseKeys(explode('/','foo/bar/baz'),$yourArray);

edit: as Jack pointed out, recursion is not needed:

function findByKey(array $keys,array $array){
    while(!is_null($key = array_shift($keys))){
        if(!isset($array[$key])) return null;
        $array = $array[$key];
    }
    return $array;
}
$values = findByKey(explode('/','foo/bar/baz'),$yourArray);

A simple loop can make it, like:

function get(array $array, $keys) {
  $val = $array;
  foreach (explode('/', $keys) as $part) {
    if (!isset($val[$part])) {
      return null;
    }
    $val = $val[$part];
  }
  return $val;
}

$array['foo']['bar']['baz'] = 'something';
echo get($array, 'foo/bar/baz');

http://ideone.com/vcRvXW

Edit:

For modification, just use references:

function set(array &$array, $keys, $value) {
  $val = &$array;
  foreach (explode('/', $keys) as $part) {
    if (!isset($val[$part])) {
      $val[$part] = array();
    }
    $val = &$val[$part];
  }
  $val = $value;
}

http://ideone.com/WUNhF6