Anyone can help me with a function or point me into the direction on cutting a multidimensional array?

Here is what I need:

$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[][];

Thanks,

share|improve this question

1  
just out of curiosity how is this useful? – amosrivera Sep 28 '11 at 16:39
1  
if $array[x][y][b][q][o][p] = 5 and $array[b][c][f][q][l][v]=3, What would your function exactly return? – Guilhem Hoffmann Sep 28 '11 at 16:40
@Guilhem Hoffmann , if sliced by 2, would return an multidimensional array with a maximum of 2 dimensions $array[][], both, $array[x][y] and $array[b][c] elements would be on that array, but any other child array would be removed. – Henrique Sep 28 '11 at 16:45
feedback

1 Answer

up vote 3 down vote accepted

You can write yourself the solution (even if I don't really understand the 'cutting' logic)

<?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;

}
?>
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.