Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. $source = array(
  3.         'INBOX',
  4.         'INBOX.lists',
  5.         'INBOX.lists.asterisk',
  6.         'INBOX.lists.asterisk.commits',
  7.         'INBOX.lists.asterisk.bugs',
  8. );
  9.  
  10.  
  11. function buildMailFolderTree(&$folders, $folder, $path = '')
  12. {
  13.         $pos = strpos($folder, '.');
  14.         if($pos !== false)
  15.         {
  16.                 //subfolder
  17.                 $subFolders = substr($folder, $pos+1);
  18.                 $currentFolder = substr($folder, 0, $pos);
  19.         }
  20.         else
  21.         {
  22.                 //toplevel folder
  23.                 $subFolders = false;
  24.                 $currentFolder = $folder;
  25.         }
  26.  
  27.         $path .= $currentFolder;
  28.  
  29.         if(!isset($folders[$currentFolder]))
  30.         {
  31.                 $folders[$currentFolder] = array(
  32.                         'id' => $path,
  33.                         'name' => $currentFolder,
  34.                         'folders' => array(),
  35.                 );
  36.         }
  37.         else
  38.         {
  39.                 // we have two folders with the same name ......
  40.         }
  41.  
  42.  
  43.         if(strlen($subFolders))
  44.         {
  45.                 buildMailFolderTree($folders[$currentFolder]['folders'], $subFolders, $path . '.');
  46.         }
  47. }
  48.  
  49. $folders = array();
  50. foreach($source as $folder)
  51. {
  52.         buildMailFolderTree($folders, $folder);
  53. }
  54.  
  55. echo "folder structure ....\n";
  56. var_dump($folders);
  57. echo "\n\n";
  58. ?>
  59.  
  60.  
  61. freebsd1% php ./foo.php
  62. folder structure ....
  63. array(1) {
  64.   ["INBOX"]=>
  65.   array(3) {
  66.     ["id"]=>
  67.     string(5) "INBOX"
  68.     ["name"]=>
  69.     string(5) "INBOX"
  70.     ["folders"]=>
  71.     array(1) {
  72.       ["lists"]=>
  73.       array(3) {
  74.         ["id"]=>
  75.         string(11) "INBOX.lists"
  76.         ["name"]=>
  77.         string(5) "lists"
  78.         ["folders"]=>
  79.         array(1) {
  80.           ["asterisk"]=>
  81.           array(3) {
  82.             ["id"]=>
  83.             string(20) "INBOX.lists.asterisk"
  84.             ["name"]=>
  85.             string(8) "asterisk"
  86.             ["folders"]=>
  87.             array(2) {
  88.               ["commits"]=>
  89.               array(3) {
  90.                 ["id"]=>
  91.                 string(28) "INBOX.lists.asterisk.commits"
  92.                 ["name"]=>
  93.                 string(7) "commits"
  94.                 ["folders"]=>
  95.                 array(0) {
  96.                 }
  97.               }
  98.               ["bugs"]=>
  99.               array(3) {
  100.                 ["id"]=>
  101.                 string(25) "INBOX.lists.asterisk.bugs"
  102.                 ["name"]=>
  103.                 string(4) "bugs"
  104.                 ["folders"]=>
  105.                 array(0) {
  106.                 }
  107.               }
  108.             }
  109.           }
  110.         }
  111.       }
  112.     }
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement