使用php(需要避免eval)使用动态生成的变量访问多维数组键

I need to avoid eval() again. I want to access a multidimensional array like this:

$items = $xml2array[$explode_path[0]][$explode_path[1]];

The thing is that $explode_path[0] and $explode_path[1] are calculated through a for loop:

for($i=0; $i<$count_explode; $i++) { }

Right now the entire code looks like this:

function getValues($contents, $xml_path) {
    $explode_path = explode('->', $xml_path);
    $count_explode = count($explode_path);
    $xml2array = xml2array($contents);

    $correct_string = '$items = $xml2array';

    for($i=0; $i<$count_explode; $i++) {
        $correct_string .= '[$explode_path['.$i.']]';
    }

    $correct_string .= ';';
    eval($correct_string);
    return $items;
}

$contents = readfile_chunked($feed_url, true);
$items = getValues($contents, 'deals->deal'); # will get deals->deal from MySQL

foreach($items as $item) {
    echo $item['deal_title']['value'].' - '.$item['dealsite']['value'].'<br />';
}

I can't figure out how I can access the $xml2array array that way:

$items = $xml2array[$explode_path[0]][$explode_path[1]];

Any help would be highly appreciated!

What about replacing your getValues() function with:

function getValues($contents, $xml_path) {
    $explode_path = explode('->', $xml_path);
    $count_explode = count($explode_path);
    $items = xml2array($contents);

    for($i=0; $i<$count_explode; $i++) {
        $items = $items[$explode_path[$i]];
    }

    return $items;
}

Edit: Cleaner version:

function getValues($contents, $xml_path) {
    $items = xml2array($contents);

    foreach(explode('->', $xml_path) as $k)
    {
        $items = $items[$k];
    }

    return $items;
}