associative array to list
am 15.09.2007 21:32:33 von lskatzHi,
I want to make an associative array become a list, and hopefully an
html list (
- ) after this is done. I made a recursive function that
isn't quite doing the job. The problem is that it is repeating the
first element of the array for some reason. Any help?
I expect the output of the function to be the following, but it's not
(see below the php code for the output).
name1
content1
name2
name2a
content2a
name2b
content2b
print "
";";
print_r($ToC); // previously defined, but shown in the output below
print "\n=============\n";
print formatDocumentation($ToC);
print "
function formatDocumentation($ToC,$level=0,$output=""){
$numMenus=count($ToC);
$name=array_keys($ToC);
$indentation=stringMultiply(" ",$level);
foreach($ToC as $thisName => $content) {
$output.= "$indentation$thisName\n";
if(is_array($content)){
$output.= formatDocumentation($content,$level+1,$output);
continue;
}
else{
$output.= "$indentation$content\n\n";
}
}
return $output;
}
function stringMultiply($str,$x){
$output="";
for($i=0;$i<$x;$i++){
$output.=$str;
}
return $output;
}
?>
OUTPUT
Array
(
[name1] => content1
[name2] => Array
(
[name2a] => content2a
[name2b] => content2b
)
)
=============
name1
content1
name2
name1
content1
name2
name2a
content2a
name2b
content2b