Jump to content


Photo

function to return key(s) for value in a multi-dimensional array


  • Please log in to reply
4 replies to this topic

#1 davepass

davepass
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 March 2006 - 07:53 AM

I'd like to be able to find the entire key for a given value in a multi-dimensional array.

So assuming an array named $structure with the following output:

Array
(
[0] => Array
(
[path] => ../content/about
[name] => about
[kind] => directory
[content] =>
)

[1] => Array
(
[path] => ../content/contact
[name] => contact
[kind] => directory
[content] =>
)

[2] => Array
(
[path] => ../content/resources
[name] => resources
[kind] => directory
[content] =>
)
}

For example, I'd like to be able to feed the function the array name ($structure) and a value of "resources" and have it return "[2][name]".


Here is what I have so far and I'm pretty stumped:

function getNavItemIndex($navItem, $haystack)
{
    $arrStr = "";
    foreach ($haystack as $k => $v) 
    { 
        if(is_array($v))
        {    
            getNavItemIndex($navItem, $v);
        }
        else
        {
             if ($v == $navItem)
             {
                $thisIndex = $k;
                break;
             }
        }
    }
    return $thisIndex;
}
I call the function on itself recursively because I'll never know how deep the array goes. So I just keep going through each one until the value is a string and not an array.

I've got 2 problems:

1. when I get a value for $thisIndex, it only reflects the key of the last array looped through. In the case of the example above it would be "name" rather than "[2][name]".

2. Even if #1 were not an issue, the function always returns an empty string, even through $thisIndex can be printed out within the function... scope issue??


Any help would be appreciated!

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 12 March 2006 - 08:04 AM

both of the issues are scope issues... in each iteration you need to 'store' each value or 'do' something with it.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 davepass

davepass
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 March 2006 - 08:07 AM

[!--quoteo(post=354124:date=Mar 12 2006, 03:04 AM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 12 2006, 03:04 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
both of the issues are scope issues... in each iteration you need to 'store' each value or 'do' something with it.
[/quote]


I'm sorry. I don't really follow what you mean. Could you elaborate?


#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 12 March 2006 - 08:51 AM

sorry. think of it this way..

when you reutnr a value from a function inside of a loop, it will always only return the last value...

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 12 March 2006 - 09:47 AM

The problem is that you arent handling the case of when youve found the parent array. Youre only looking for correct values. You have to be able to single all the way up the stack that its succeeded.


function getNavItemIndex($navItem, $haystack)
{
   $thisIndex = false; //default case
   foreach ($haystack as $k => $v)
   {
      if (is_array($v))
      {
         $recurNdx = getNavItemIndex($navItem, $v);
         if ($recurNdx !== false) //found in recursive call
            return "[$k]$recurNdx";
      } else
         if ($v == $navItem) //found here.
            return "[$k]";
   }
   return $thisIndex;
}

Should return "[2][name]"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users