Php 将数组索引数组转换为动态大小的多维数组中的值

Php 将数组索引数组转换为动态大小的多维数组中的值,php,arrays,multidimensional-array,indices,Php,Arrays,Multidimensional Array,Indices,我有一个数组,如下所示: test[ 0 => 0, 1 => 2 , 2 => 0, 3 => 2 ] 上面的数组值是一个较大数组的索引表示,因此我需要将这些值转换为另一个数组的索引,该数组将映射较大数组,如下所示: test2[0][2][0][2] 我试过: $test3= array_flip ( $test ) 工作正常,但由于我无法控制数组,因此冲突不方便,是否有帮助?函数arrayToIndex($array,$index){ function ar

我有一个数组,如下所示:

test[
0 => 0,
1 => 2 ,
2 => 0,
3 => 2
]
上面的数组值是一个较大数组的索引表示,因此我需要将这些值转换为另一个数组的索引,该数组将映射较大数组,如下所示:

test2[0][2][0][2]
我试过:

$test3= array_flip ( $test ) 
工作正常,但由于我无法控制数组,因此冲突不方便,是否有帮助?

函数arrayToIndex($array,$index){
function arrayToIndex($array,$index) {
        $element = $array;
            for ($i = 0; $i < count($index); $i++) {
                    $element = $element[$index[$i]];
            }
            return $element;
    }
$test = [[[[1],[2]],[[3],[4]]],[[[5],[6]],[[7],[8]]]];
$index = [0,1,0,0];
echo arrayToIndex($test,$index);
$element=$array; 对于($i=0;$i
下面是一个函数,它实现了您要求的行为
$array
是要搜索的数组,
$index
是索引数组。我建议您使用这个示例,看看如何检索每个值


实例:

因此,在head之后,我提出了以下功能,达到了我的目的:

$mainarray = [

0 => [
   0 =>[
      0 => [
         0 => 'one' ,
         1 => 'two' ,
         2 => 'three' ,
         3 => 'four'
      ],

      1 => [
         0 => 'one' ,
         1 => 'two' ,
         2 => 'three' ,
         3 => 'four'
      ]


   ]
]

]
然后,下面这个数组的值作为
$mainarray
的索引,上面是我想要得到的,比如说
one
,如上所述

$arraywithseacrhindex = [
0 => 0 , 
1 => 0 ,
2 => 0 ,
3 => 0 ,
]
解决方案:

$array = $mainarray ;
$arr = $arraywithseacrhindex ;
$counter= count($arr)-1 ;
$result = array() ;
for($i=0; $i< $counter; $i++) {

    if(empty( $result ) )
    {
        // first loop to put $result at per with $array
        $result[$arr[$i]] = $array[$arr[$i]]  ; 
    }else{
            // this is the trick of the solution
            $cResult = current($result);
            unset($result);
            $result = array() ;
            $result[$arr[$i]] = $cResult[$arr[$i]]  ; 




    }

}

var_dump($result);
$array=$mainarray;
$arr=$arraywithseacrhindex;
$counter=计数($arr)-1;
$result=array();
对于($i=0;$i<$counter;$i++){
if(空($result))
{
//使用$array将$result置于per的第一个循环
$result[$arr[$i]]=$array[$arr[$i]];
}否则{
//这就是解决方案的诀窍
$cResult=当前($result);
未结算($结果);
$result=array();
$result[$arr[$i]]=$cResult[$arr[$i]];
}
}
var_dump($结果);

希望它对将来的人有所帮助。

您正在尝试将元素数组转换为数组。换句话说,您正在尝试创建一个嵌套数组。没有内置的php函数来点他的。您必须自己编写。
test2[test[0]][test[1]][test[2]][test[3]]
not work?@JamesPaterson可以,但我不知道数组的长度,因为它的dynamicOk-处理函数返回3,我的最终任务是获得数组索引表示,如示例中的
$index
中所示。这是对测试数据
$test
$index
进行处理。在您的OP示例中,您需要只复制函数并调用函数作为
arrayToIndex($test2,$test)
。它返回三,因为这是
$test[0][1][0][0]
处的元素。同样,
$test2
$test1
值的最终结果,我知道从
$test
的值中获取
$test2
需要什么
$test2