Array: how to find a parent key when using a child key?

From: Date: Thu, 29 Sep 2005 14:32:54 +0000
Subject: Array: how to find a parent key when using a child key?
Groups: php.general 
Request: Send a blank email to php-general+get-223411@lists.php.net to get a copy of this message
I'm using the recursive function and it use the foreach() loop. I can figure out how to use the current key but I could not figure out how to use the parent's key. Can anyone point out to what I'm missing here? Just look at the "//Parent Key - How??" comment in the script... I'm not sure what php function that can allow us to walk up the array like array_walk() for example. Thanks... [code] $array = array ( 'NEWSFEED' => array ( '0' => array ( 'MESSAGE' => array ( '0' => array ( 'COMMENT' => array ( '0' => array ( 'VALUE' => 'Comment #1' ) ) ), '1' => array ( 'COMMENT' => array ( '0' => array ( 'VALUE' => 'Comment #2' ) ) ) ) ) ) ); function GetXmlStr($tree) { $branch_level = 0; //Tree's branches level... XmlTreeDecompile($tree, $branch_level); return htmlentities($str1); } function XmlTreeDecompile($tree, $branch_level) { $branch_level++; foreach($tree as $key => $value) { if ($branch_level % 2 != 0) { //xml name-tag counter... if(!(is_array($value))) { echo str_repeat(" ",$branch_level)."<".$key.">".$value."</".$key.">"."\r\n"; } else { echo str_repeat(" ",$branch_level)."<".$key.">"."\r\n"; XmlTreeDecompile($value, $branch_level); echo str_repeat(" ",$branch_level)."</".$key.">"."\r\n"; $str1 = $key; } } else { //Xml tag's name... //echo $key." #<br>"; if ($key != 0) { //Parent Key - How?? echo str_repeat(" ",($branch_level-1))."</MESSAGE###>"."\r\n"; echo str_repeat(" ",($branch_level-1))."<MESSAGE###>"."\r\n"; } XmlTreeDecompile($value, $branch_level); } } } $xml_string = GetXmlStr($array); //echo "\n<br><br>\n".$xml_string; [/code]

« previous php.general (#223411) next »