?
Solved

Get pathway by key in multidimensional array ( PHP )

Posted on 2008-10-14
5
Medium Priority
?
812 Views
Last Modified: 2013-12-13
Hi , how can generate a pathway from a element in array having it's key ?

 
Look at the array below ,

i need function for example
get_path($id) that will return on requested $id = 314  path to it  => : 262,304,312,314

Thanks for all your help.

[262] => Array
(
    [266] => Array
        (
            [288] => 266
            [289] => 266
            [290] => 266
            [291] => 266
            [293] => 266
        )
 
    [267] => 262
    [304] => Array
        (
            [310] => 304
            [312] => Array
                (
                    [314] => 312
                )
 
            [313] => 304
        )
 
    [305] => 262
    [306] => 262
    [311] => 262
)

Open in new window

0
Comment
Question by:tajny
[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
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
dmadole earned 1500 total points
ID: 22716928
I can't think of anything cleverm but the direct approach attached gives the result you want.

<?
 
$sample = array(
  262 => array(
    266 => array(
      288 => 266,
      289 => 266,
      290 => 266,
      291 => 266,
      293 => 266
    ),
    267 => 262,
    304 => array(
      310 => 304,
      312 => array(
        314 => 312
      ),
      313 => 304
    )
  )
);
 
function pathway($array, $find) {
 
    foreach ($array as $key => $value) {
 
        if (is_array($value)) {
 
            $subkey = pathway($value, $find);
 
            if (isset($subkey)) return "$key,$subkey";
        }
        else if ($key === $find) return $key;
    }
    return NULL;
}
 
echo pathway($sample, 314);
 
?>

Open in new window

0
 

Author Closing Comment

by:tajny
ID: 31506070
As simple as is :] Thank you very much!
0
 

Author Comment

by:tajny
ID: 22726927
There is a problem with that function you posted , when a searchable element has array in value it returns null ;/

$sample = array(
  262 => array(
    266 => array(
      288 => 266,
      289 => 266,
      290 => 266,
      291 => 266,
      293 => 266
    ),
    267 => 262,
    304 => array(
      310 => 304,
      312 => array(
        314 => 312
      ),
      313 => 304
    )
  )
);
 
echo pathway($sample,266) //gives null 
echo pathway($sample,304) //gives null 
echo pathway($sample,312) //gives null 
echo pathway($sample,310) //ok
echo pathway($sample,267) //ok

Open in new window

0
 
LVL 7

Expert Comment

by:dmadole
ID: 22726968
Then I guess it's just right for the B grade you gave it:

"Grading at Experts Exchange is not like school. It's more like the "10-point Must" system in professional boxing; in other words, an answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade."
0
 

Author Comment

by:tajny
ID: 22739289
Figuret it out, we should check is $key == $find before checking  it's an array  , and do a casted comparison or use == instead

and it works.

Greetz.

function pathway(&$array, $find) {
    foreach ($array as $key => $value) {
    	if ((int)$key == (int)$find) { 
    		return $key;
    	} else 
        if (is_array($value)) {
            $subkey = pathway($value, $find);
            if (isset($subkey)) 
            return "$key,$subkey";
        }
    }
    return NULL;
}

Open in new window

0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Suggested Courses

764 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