Php 值的Concat数组和字符串变量

Php 值的Concat数组和字符串变量,php,multidimensional-array,Php,Multidimensional Array,我有一个变量$temp,它包含: ['test1']['test2']['test3'] 我想在数组中获取此变量的值:$array。 我尝试了$array[$temp]和其他可能性 getNavItemIndex返回一个数组,其中包含所请求值的位置 编辑: $input = array(); $input['level0']['level1']['somekey'] = "value of somekey"; $input['level0']['level1']['somekey2'] = "

我有一个变量
$temp
,它包含:

['test1']['test2']['test3']
我想在数组中获取此变量的值:
$array

我尝试了
$array[$temp]
和其他可能性

getNavItemIndex
返回一个数组,其中包含所请求值的位置

编辑:

$input = array();
$input['level0']['level1']['somekey'] = "value of somekey";
$input['level0']['level1']['somekey2'] = "value of somekey2";
$input['level0']['level1b']['somekey1b'] = "value of somekey1b";
$input['level0']['level1']['level2']['somekey1c'] = "value of somekey1c";
$json1 = getNavItemIndex($input, "value of somekey1c");  
foreach ($json1 as $key => $value) {
    $temp .= "['";
    $temp .= $value; 
    $temp .= "']";   
}
echo $temp; // ['level0']['level1']['level2']['somekey1c']
echo $input[$temp]; //value of somekey1c

编辑后更改了答案

$output = null;
for($i = 0; $i < count($temp); $i++) {
    if($output === null && isset($input[$temp[$i]])) {
        $output = $input[$temp[$i]];
    }elseif (isset($output[$temp[$i]])) {
        $output = $output[$temp[$i]];
    }else {
        $output = null;
    }
}
echo $output;
更新一个更具说服力和可重用的解决方案

function getByKeys($arr, $keys) {
    $key = array_shift($keys);
    if(isset($arr[$key])) {
        if(count($keys) > 0) {
            return getByKeys($arr[$key], $keys);
        }else {
            return $arr[$key];
        }
    }else {
        throw new Exception('Key is not isset');
    }
}

echo getByKeys($input, $temp);

编辑后更改了答案

$output = null;
for($i = 0; $i < count($temp); $i++) {
    if($output === null && isset($input[$temp[$i]])) {
        $output = $input[$temp[$i]];
    }elseif (isset($output[$temp[$i]])) {
        $output = $output[$temp[$i]];
    }else {
        $output = null;
    }
}
echo $output;
更新一个更具说服力和可重用的解决方案

function getByKeys($arr, $keys) {
    $key = array_shift($keys);
    if(isset($arr[$key])) {
        if(count($keys) > 0) {
            return getByKeys($arr[$key], $keys);
        }else {
            return $arr[$key];
        }
    }else {
        throw new Exception('Key is not isset');
    }
}

echo getByKeys($input, $temp);


您能发布到目前为止的代码吗?并发布预期的输出如果它看起来像$temp[0]=“test1”、$temp[1]=“test2”&$temp[2]=“test3”?我已经编辑过,我想要这个预期的输出:somekey1c的值我已经根据你的更新更新了我下面的答案。你能发布到目前为止的代码吗?如果它看起来像$temp[0]='test1',$temp[1]='test2'和$temp[2]='test3',我已经编辑过,我想要这个预期的输出:somekey1c的值我已经根据你的更新更新了我下面的答案。你能发布到目前为止的代码吗?如果它看起来像$temp[0]='test1',$temp[1]='test2'和$temp[2]='test3',我已经编辑过,我想要这个预期的输出:SomeKey1的价值我已经根据你的更新更新了我下面的答案。我想要的是更简单的,但它可以工作谢谢:)我想新的解决方案会更简单一点,也更可重用:)我想要的是更简单,但它可以工作谢谢:)我想新的解决方案会更简单一点,还有,它的可重用性更强:)我一直在寻找更简单的解决方案,但它可以工作。谢谢:)我认为新的解决方案会更简单一点,而且它的可重用性更强:)