associative array to list

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
lskatz [ Sa, 15 September 2007 21:32 ] [ ID #1821215 ]

Re: associative array to list

Ah, nevermind, I realized I was adding onto my output variable twice.
Here's the fixed up function

function formatDocumentation($ToC,$level=0){
if($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);
}
else{
$output.= "$indentation$content\n\n";
}
}
return $output;
}
lskatz [ Sa, 15 September 2007 21:45 ] [ ID #1821216 ]

Re: associative array to list

"lskatz" <lskatz [at] gmail.com> wrote in message
news:1189885512.959260.307230 [at] 19g2000hsx.googlegroups.com...
> Ah, nevermind, I realized I was adding onto my output variable twice.
> Here's the fixed up function
>
> function formatDocumentation($ToC,$level=0){
> if($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);
> }
> else{
> $output.= "$indentation$content\n\n";
> }
> }
> return $output;
> }



:)
Mike



--------------------------------------------------
http://www.documentscanningbureau.com
Hans-Peter Sauer [ Mo, 15 Oktober 2007 14:11 ] [ ID #1845448 ]
PHP » alt.php » associative array to list

Vorheriges Thema: window close event
Nächstes Thema: Looking to Expand