nikefido Posted March 20, 2008 Share Posted March 20, 2008 I have some javascript that uses unordered lists to make a collapsing file structure...so i need an unordered list to be in a certain format. It needs to start and end with a UL /UL (duh). Each Folder is a LI and each file is a LI of a sub-UL. my function: <?php function print_array($a, $n=0) { //echo "<li>\n"; //echo "</ul>\n"; $first = true; foreach ($a as $key => $value) { if (is_array($value)) { if($n != 0) {echo "</ul>\n</li>\n";} $n++; echo "<li><a href=\"#\">Folder</a>\n"; $first = true; $this->print_array($value, $n); } else { if($first) echo "<ul>\n"; $first = false; echo "<li><a href=\"#\">File</a></li>\n"; } } ?> This prints out a UL that is does show imbedded/sub directories (they are all on the same level!) However, the folder structure is like this: Folder1 SomeFile1.0.1 Folder1.5 Somefile1.5.1 Folder2 SomeFile2.0.1 Folder2.5 SomeFile2.5.1 Folder3 SomeFile3.0.1 What am I missing here? Link to comment https://forums.phpfreaks.com/topic/97151-need-logic-helpunordered-list-from-array/ Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 post the output from var_export($a); so we can see what the input array looks like Link to comment https://forums.phpfreaks.com/topic/97151-need-logic-helpunordered-list-from-array/#findComment-497245 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2008 Share Posted March 21, 2008 I wrote the following, see if it does what you want: <?php $ary = array('L1' => 'L1', 'L2' => array('L2.1' => 'L2.1', 'L2.2' => 'L2,2', 'L2.3' => array('L2.3.1'=>'L2.3.1', 'L2.3.2'=>'L2.3.2'), 'L2.4'=>'L2.4'), 'L3' => 'L3'); function ul($a) { foreach ($a as $k => $v) { echo '<li>' . $k; if (is_array($v)) { echo '<ul>'."\n"; ul($v); echo '</ul>'."\n"; } echo "</li>\n"; } } echo "<ul>\n"; ul($ary); echo "</ul>\n"; ?> This produces * L1 * L2 o L2.1 o L2.2 o L2.3 + L2.3.1 + L2.3.2 o L2.4 * L3 Ken Link to comment https://forums.phpfreaks.com/topic/97151-need-logic-helpunordered-list-from-array/#findComment-497249 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.