Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $source = array(
- 'INBOX',
- 'INBOX.lists',
- 'INBOX.lists.asterisk',
- 'INBOX.lists.asterisk.commits',
- 'INBOX.lists.asterisk.bugs',
- );
- function buildMailFolderTree(&$folders, $folder, $path = '')
- {
- $pos = strpos($folder, '.');
- if($pos !== false)
- {
- //subfolder
- $subFolders = substr($folder, $pos+1);
- $currentFolder = substr($folder, 0, $pos);
- }
- else
- {
- //toplevel folder
- $subFolders = false;
- $currentFolder = $folder;
- }
- $path .= $currentFolder;
- if(!isset($folders[$currentFolder]))
- {
- $folders[$currentFolder] = array(
- 'id' => $path,
- 'name' => $currentFolder,
- 'folders' => array(),
- );
- }
- else
- {
- // we have two folders with the same name ......
- }
- if(strlen($subFolders))
- {
- buildMailFolderTree($folders[$currentFolder]['folders'], $subFolders, $path . '.');
- }
- }
- $folders = array();
- foreach($source as $folder)
- {
- buildMailFolderTree($folders, $folder);
- }
- echo "folder structure ....\n";
- var_dump($folders);
- echo "\n\n";
- ?>
- freebsd1% php ./foo.php
- folder structure ....
- array(1) {
- ["INBOX"]=>
- array(3) {
- ["id"]=>
- string(5) "INBOX"
- ["name"]=>
- string(5) "INBOX"
- ["folders"]=>
- array(1) {
- ["lists"]=>
- array(3) {
- ["id"]=>
- string(11) "INBOX.lists"
- ["name"]=>
- string(5) "lists"
- ["folders"]=>
- array(1) {
- ["asterisk"]=>
- array(3) {
- ["id"]=>
- string(20) "INBOX.lists.asterisk"
- ["name"]=>
- string(8) "asterisk"
- ["folders"]=>
- array(2) {
- ["commits"]=>
- array(3) {
- ["id"]=>
- string(28) "INBOX.lists.asterisk.commits"
- ["name"]=>
- string(7) "commits"
- ["folders"]=>
- array(0) {
- }
- }
- ["bugs"]=>
- array(3) {
- ["id"]=>
- string(25) "INBOX.lists.asterisk.bugs"
- ["name"]=>
- string(4) "bugs"
- ["folders"]=>
- array(0) {
- }
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement