Jump to content

Archived

This topic is now archived and is closed to further replies.

pbjorge12

Sorting through Multi-Dimensional Array

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.