Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php切割多维数组_Php_Arrays_Multidimensional Array - Fatal编程技术网

php切割多维数组

php切割多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,有人能帮我做一个函数,或者给我指出切割多维数组的方向吗 以下是我需要的: $array[x][y][b][q][o][p]; $array[b][c][f][q][l][v]; $newArray = cut_array_depth($array, 2); // Would return a new array with a maximum dimension of 2 elements // all others would be left out $newArray[][]; 谢谢,您

有人能帮我做一个函数,或者给我指出切割多维数组的方向吗

以下是我需要的:

$array[x][y][b][q][o][p];
$array[b][c][f][q][l][v];

$newArray = cut_array_depth($array, 2);

// Would return a new array with a maximum dimension of 2 elements
// all others would be left out
$newArray[][];

谢谢,

您可以自己编写解决方案(即使我并不真正理解“切割”逻辑)



只是出于好奇,这有什么用处?如果$array[x][y][b][q][o][p]=5和$array[b][c][f][q][l][v]=3,您的函数会返回什么?如果将其切片为2,将返回一个最大为2维的多维数组$array[],那么,$array[x][y]和$array[b][c]元素都将位于该数组上,但任何其他子数组都将被删除。
<?php
function cut_array_depth($array, $depth, $currDepth = 0){
    if($currDepth > $dept){
        return null;  
    }
   $returnArray = array();
   foreach( $array as $key => $value ){        
      if( is_array( $value ) ){              
          $returnArray[$key] = cut_array_depth($value, $depth , $currDepth +1);
      } else {
          $returnArray[$key] = $value;
   }
   return $returnArray;

}
?>