Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. ##############
  2. ## GET TREE ##
  3. ##############
  4.  
  5. $tree = $em->createQuery('SELECT l FROM Entity\Location l ORDER BY l.lft ASC')->getResult();
  6.        
  7. $result = TreeHelper::getOptions($tree);
  8.  
  9.  
  10. #################
  11. ## TREE HELPER ##
  12. #################
  13.  
  14. <?php
  15. namespace Helper;
  16.    
  17. class TreeHelper
  18. {
  19.     public static function getUL($tree = array())
  20.     {
  21.         $result = '';
  22.         $depth = -1;
  23.         while (!empty($tree)) {
  24.             $node = array_shift($tree);
  25.  
  26.             if ($node->getLvl() > $depth) {
  27.                 $result .= '<ul>';
  28.             }
  29.  
  30.             if ($node->getLvl() < $depth) {
  31.                 $result .= str_repeat('</ul>', $depth - $node->getLvl());
  32.             }
  33.  
  34.             $result .= '<li>' . $node->getTitle() . '</li>';
  35.  
  36.             $depth = $node->getLvl();
  37.  
  38.             if (empty($tree)) {
  39.                 $result .= str_repeat('</ul>', $depth + 1);
  40.             }
  41.         }
  42.  
  43.         return $result;
  44.     }
  45.  
  46.     public static function getOptions($tree = array())
  47.     {
  48.         $options = $reference = array();
  49.         $depth = -1;
  50.         $parent = null;
  51.        
  52.         while (!empty($tree)) {
  53.             $node = array_shift($tree);
  54.  
  55.             $parent = &$options[$node->getUrl()];
  56.  
  57.             $parent_id = null !== $node->getParent() ? $node->getParent()->getUrl() : 0;
  58.  
  59.             if ($node->getLvl() == 0) {
  60.                 $list[$node->getUrl()] = &$parent;
  61.             } else {
  62.                 $options[$parent_id][$node->getUrl()] = &$parent;
  63.             }
  64.  
  65.         }
  66.  
  67.         return $options;
  68.     }
  69. }
  70.  
  71.  
  72. ############
  73. ## RESULT ##
  74. ############
  75.  
  76. Array
  77. (
  78.     [usa] => Array
  79.         (
  80.             [california] => Array
  81.                 (
  82.                     [san-diego] =>
  83.                     [los-angeles] =>
  84.                     [san-francisco] =>
  85.                 )
  86.  
  87.         )
  88.  
  89.     [california] => Array
  90.         (
  91.             [san-diego] =>
  92.             [los-angeles] =>
  93.             [san-francisco] =>
  94.         )
  95.  
  96.     [san-diego] =>
  97.     [los-angeles] =>
  98.     [san-francisco] =>
  99. )
  100.  
  101.  
  102. ##############
  103. ## QUESTION ##
  104. ##############
  105.  
  106. I'm almost getting the desired result set that I want.  My getUL method works, but now I need to grab put the data into an array.  I'm needing something more along the lines of what's below:
  107.  
  108. Array
  109. (
  110.    [usa] => Array
  111.        (
  112.            [california] => Array
  113.                (
  114.                    [san-diego] =>
  115.                    [los-angeles] =>
  116.                    [san-francisco] =>
  117.                )
  118.  
  119.        )
  120. )
  121.  
  122. I will also be needing to expand on this so that I can do (unlimited parent / child levels):
  123.  
  124. Array
  125. (
  126.        [usa] => Array
  127.        (
  128.            [california] => Array
  129.                (
  130.                    [san-diego] =>
  131.                    [los-angeles] => Array
  132.                        (
  133.                            [north] =>
  134.                            [west] =>
  135.                        )
  136.                    [san-francisco] =>
  137.                )
  138.  
  139.        )
  140.    [mexico] => Array
  141.        (
  142.            [cabo-san-lucas] =>
  143.            [cancun] =>
  144.        )
  145. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement