The problem with array values

I am having a problem where a subroutine (which I cannot change) returns me either a 3 nested array or a 2 nested array. The keyword values ​​are never the same, however I would like to normalize the nesting so that I can make two nested arrays 3 levels each time to avoid "Note: Undefined index:". Or, if possible, try counting the number of levels deep in the array so that I can code the code accordingly.

0


a source to share


3 answers


You can use isset () to determine if a particular level is present in the array. If not, add it.



+3


a source


function get_depth($arr) {
   foreach ( $arr as $arr2 ) {
     if ( is_array($arr2) ) {
       return 1+get_depth($arr2);
     }
     break;
   }
   return 1;
}

      



+1


a source


Ok, this answer will really depend on what you are doing. Why not just check if a nested array exists?

if (isset($val[3][2])) {
  ....
}

      

0


a source







All Articles