不是很漂亮(也不是递归的,这是一件好事),但应该可以:
// Input data
$array = array(
"collection" => "departments",
"action" => "find",
"args" => array ("_id" => array('$in' => "{{variablename}}"))
);
// Create a stack
$stack = array(&$array);
// Loop until the stack is empty
while (sizeof($stack) > 0) {
// Get the first variable in the stack (by reference)
foreach ($stack as &$current) break;
// Remove the first variable from the stack (by reference, same as array_shift but array_shift breaks the references)
$stack = array_slice($stack, 1);
// If the shifted variable is an array
if (is_array($current)) {
// Add all the array's values to the stack (by reference)
foreach ($current as &$value) {
$stack[] =& $value;
}
}
// If the shifted variable is the one we want
elseif ($current == '{{variablename}}') {
// Stop the loop, leaving $current as the reference to the variable we want
break;
}
}
$current = 'test';
var_dump($current);
var_dump($array);
示例:http: //ideone.com/hEzZk