Solved

Determining how 'deep' an element of a multidimensional array is

Posted on 2009-04-02
2
195 Views
Last Modified: 2012-05-06
I need to determine how 'deep' an element is within a multidimensional array. For example, following the code snippet, how can I determine whether an element is in the first level, the second level, the third level, etc.? How can I determine that "Vancouver" is nested three levels deep?

Ideally I would be looking for a return of an integer - so looking for "Countries" would return 0, "USA" would return 1, "Seattle" would return 2, etc.
$myArray = array(

		"Countries" => array(

				"Canada" => array(

						"Vancouver", "Toronto"),

				"USA" => array(

						"New York", "Seattle"),

				     ),

		);

Open in new window

0
Comment
Question by:radarhill
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24055662
Try this:
function howDeep($a,$target,$level=0) {

  if(in_array($target,array_keys($a)))

    return $level;

  foreach($a as $key=>$sub_array) {

    if(is_array($sub_array)) {

      $tmp = howDeep($sub_array,$target,$level+1);

      if($tmp) 

        return $tmp;

    } elseif($sub_array==$target)

      return $level;

  }

  return false;

}

echo howDeep($myArray,'Seattle');

Open in new window

0
 
LVL 1

Author Comment

by:radarhill
ID: 24056056
Perfect - thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now