Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-02
2
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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