You are here: Array: how to find a parent key when using a child key? « PHP « IT news, forums, messages
Array: how to find a parent key when using a child key?

Posted by "Scott Fletcher" on 09/29/05 17:32

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]

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация