You are here: associative array to list « All PHP « IT news, forums, messages
associative array to list

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]


Удаленная работа для программистов  •  Как заработать на 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

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