|
Posted by lskatz on 09/15/07 19:32
Hi,
I want to make an associative array become a list, and hopefully an
html list (<ul>) 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 "<pre>";
print_r($ToC); // previously defined, but shown in the output below
print "\n=============\n";
print formatDocumentation($ToC);
print "</pre>";
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
Navigation:
[Reply to this message]
|