0

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 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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])) {
  ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.