pbjorge12 Posted April 13, 2006 Share Posted April 13, 2006 I have a PHP function that recieves a file path and returns an array like below...[code]Array( [index.php] => /usr/local/www/index.php [js] => Array ( [async.js] => /usr/local/www/js/async.js [dom.js] => /usr/local/www/js/dom.js [effects.js] => /usr/local/www/js/effects.js [prototype.js] => /usr/local/www/js/prototype.js ) [logo.png] => /usr/local/www/logo.png [server.php] => /usr/local/www/server.php [test.js] => /usr/local/www/test.js) [/code]How do I sort through that array and return the HTML similar to below?[code]<ul id="fileList"><li id="firstLevel">index.php</li><li id="firstLevel">js</li><li id="secondLevel">asnyc.js</li><li id="secondLevel">dom.js</li><li id="secondLevel">effects.js</li><li id="secondLevel">prototype.js</li><li id="firstLevel">logo.png</li><li id="firstLevel">server.php</li><li id="firstLevel">test.js</li></ul>[/code]Thank you! Link to comment https://forums.phpfreaks.com/topic/7360-sorting-through-multi-dimensional-array/ Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 Try[code]$data = array( 'index.php' => '/usr/local/www/index.php', 'js' => array ( 'async.js' => '/usr/local/www/js/async.js', 'dom.js' => '/usr/local/www/js/dom.js', 'effects.js' => '/usr/local/www/js/effects.js', 'prototype.js' => '/usr/local/www/js/prototype.js' ), 'logo.png' => '/usr/local/www/logo.png', 'server.php' => '/usr/local/www/server.php', 'test.js' => '/usr/local/www/test.js');function listArray($arr, $level=0) { foreach($arr as $item) { if (is_array($item)) { listArray($item, $level+1); } else { $id = $level==0 ? 'firstlevel':'secondlevel'; echo "<li id='$id'>$item</li><br>\n"; } }}echo "<ul id='fileList'>\n";listArray($data);echo "</ul>";[/code] Link to comment https://forums.phpfreaks.com/topic/7360-sorting-through-multi-dimensional-array/#findComment-26816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.