任何人都可以帮助我实现一个函数或为我指明切割多维数组的方向吗?
这是我需要的:
$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[][];
谢谢
最佳答案
您可以自己编写解决方案(即使我不太理解“切割”逻辑)
<?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;
}
?>
关于php切割多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586550/