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
2009-05-13 13:01:05
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
2009-05-13 13:01:27
+1
A:
function get_depth($arr) {
foreach ( $arr as $arr2 ) {
if ( is_array($arr2) ) {
return 1+get_depth($arr2);
}
break;
}
return 1;
}
zilupe
2009-05-13 13:02:42