开发者

How to get hierarchy path of an element in an Array

I always want to get t开发者_StackOverflow社区he exact path of an element in an array. Example array:

array(a=>'aaa', 'b'=> array ('bbb1', 'bbb2' => array('bbb3', 'bbb4')));

So, for reaching to 'bbb4', I need to go through (b => bbb2 => bbb4). How to get this path in multidimensional array?


function get_from_array($toBeSearchedArray , $searchValue , &$exactPath)
 {
        foreach($toBeSearchedArray as $key=>$value)
        {
                  if(is_array($value) && count($value) > 0)
               {
                       $found = get_from_array($value , $searchValue , $exactPath);
                       if($found)
                       {
                            $exactPath = $key."=>".$exactPath;
                            return TRUE;
                       }
               }
               if($value == $searchValue)
               {
                      $exactPath = $value;
                      return true;
               }
        }
        return false;
 }

$exactPath = "";
$argArray = array('a'=>'aaa', 'b'=> array ('bbb1', 'bbb2' => array('bbb3', 'bbb4')));
get_from_array($argArray , "bbb4" , $exactPath); 
echo $exactPath;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜