Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############
- ## GET TREE ##
- ##############
- $tree = $em->createQuery('SELECT l FROM Entity\Location l ORDER BY l.lft ASC')->getResult();
- $result = TreeHelper::getOptions($tree);
- #################
- ## TREE HELPER ##
- #################
- <?php
- namespace Helper;
- class TreeHelper
- {
- public static function getUL($tree = array())
- {
- $result = '';
- $depth = -1;
- while (!empty($tree)) {
- $node = array_shift($tree);
- if ($node->getLvl() > $depth) {
- $result .= '<ul>';
- }
- if ($node->getLvl() < $depth) {
- $result .= str_repeat('</ul>', $depth - $node->getLvl());
- }
- $result .= '<li>' . $node->getTitle() . '</li>';
- $depth = $node->getLvl();
- if (empty($tree)) {
- $result .= str_repeat('</ul>', $depth + 1);
- }
- }
- return $result;
- }
- public static function getOptions($tree = array())
- {
- $options = $reference = array();
- $depth = -1;
- $parent = null;
- while (!empty($tree)) {
- $node = array_shift($tree);
- $parent = &$options[$node->getUrl()];
- $parent_id = null !== $node->getParent() ? $node->getParent()->getUrl() : 0;
- if ($node->getLvl() == 0) {
- $list[$node->getUrl()] = &$parent;
- } else {
- $options[$parent_id][$node->getUrl()] = &$parent;
- }
- }
- return $options;
- }
- }
- ############
- ## RESULT ##
- ############
- Array
- (
- [usa] => Array
- (
- [california] => Array
- (
- [san-diego] =>
- [los-angeles] =>
- [san-francisco] =>
- )
- )
- [california] => Array
- (
- [san-diego] =>
- [los-angeles] =>
- [san-francisco] =>
- )
- [san-diego] =>
- [los-angeles] =>
- [san-francisco] =>
- )
- ##############
- ## QUESTION ##
- ##############
- 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:
- Array
- (
- [usa] => Array
- (
- [california] => Array
- (
- [san-diego] =>
- [los-angeles] =>
- [san-francisco] =>
- )
- )
- )
- I will also be needing to expand on this so that I can do (unlimited parent / child levels):
- Array
- (
- [usa] => Array
- (
- [california] => Array
- (
- [san-diego] =>
- [los-angeles] => Array
- (
- [north] =>
- [west] =>
- )
- [san-francisco] =>
- )
- )
- [mexico] => Array
- (
- [cabo-san-lucas] =>
- [cancun] =>
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement