|
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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 ? 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 |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
"lskatz" <lskatz@gmail.com> wrote in message news:1189885512.959260.307230@19g2000hsx.googlegro ups.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 |
|
![]() |
| Outils de la discussion | |
|
|