tags:

views:

51

answers:

3

I am having a problem where a routine (that I cannot modify) is returning me either a 3 nested array -or- a 2 nested array. The key values are never the same, however, I'd like to normalize the nesting so that I can make the 2 nested array 3 levels deep every time to avoid "Notice: Undefined index:" errors. Or if possible, have a routine to count the number of levels deep the array is so I can code accordingly.

+3  A: 

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

rikh
A: 

Well, this answer is really going to depend on what you're doing. Why not simply check to see if the nested array exists?

if (isset($val[3][2])) {
  ....
}
altCognito
+1  A: 
function get_depth($arr) {
   foreach ( $arr as $arr2 ) {
     if ( is_array($arr2) ) {
       return 1+get_depth($arr2);
     }
     break;
   }
   return 1;
}
zilupe