6

Let's dig in the main problem right away, I have the input like this

$category = array(
  'A' => array('left' => 1, 'right' => 8),
  'B' => array('left' => 2, 'right' => 3),
  'C' => array('left' => 4, 'right' => 7),
  'D' => array('left' => 5, 'right' => 6),
  'E' => array('left' => 9, 'right' => 10),
);

I want the output to be something like this

$tree = array(
  array('A', 'B'),
  array('A', 'C', 'D'),
  array('E'),
);

which one is the best and fast function to loop though the input array and create the output result like this ?

10
  • You should include the business logic of conversion from the first structure to second. It is not very clear right now.
    – hw.
    Jun 8, 2013 at 12:31
  • 1
    Your desired output does not reflect a nested set. should be more like array(A => array(B => null, C => array(D => null), E => null) Jun 8, 2013 at 12:32
  • @hw the business logic here is en.wikipedia.org/wiki/Nested_set_model Jun 8, 2013 at 12:37
  • @Steven Moseley: that output is what make the function hard thought. Jun 8, 2013 at 12:39
  • @quocnguyen.clgt - I don't understand your comment. Are you saying 1) that it's difficult to create output in the format I showed above, or 2) that using output in that format is difficult? I don't see why it would make sense to output nested set data in something other than a nested set. Your desired output above looks like it would be more difficult to use than properly structured data Jun 8, 2013 at 12:45

5 Answers 5

18

Working with a nested set is a perfect case for recursion.

Given your data:

$category = array(
    'A' => array('left' => 1, 'right' => 9),
    'B' => array('left' => 2, 'right' => 4),
    'C' => array('left' => 5, 'right' => 8),
    'D' => array('left' => 6, 'right' => 7),
    'E' => array('left' => 10, 'right' => 11),
);

The following will break your nested set data down into a properly nested array in PHP:

function createTree($category, $left = 0, $right = null) {
    $tree = array();
    foreach ($category as $cat => $range) {
        if ($range['left'] == $left + 1 && (is_null($right) || $range['right'] < $right)) {
            $tree[$cat] = createTree($category, $range['left'], $range['right']);
            $left = $range['right'];
        }
    }
    return $tree;
}

$tree = createTree($category);
print_r($tree);

Output:

Array
(
    [A] => Array
        (
            [B] => Array
                (
                )

            [C] => Array
                (
                    [D] => Array
                        (
                        )

                )

        )

    [E] => Array
        (
        )

)

Then you can flatten your proper tree into the format you want with the following:

function flattenTree($tree, $parent_tree = array()) {
    $out = array();
    foreach ($tree as $key => $children) {
        $new_tree = $parent_tree;
        $new_tree[] = $key;
        if (count($children)) {
             $child_trees = flattenTree($children, $new_tree);
            foreach ($child_trees as $tree) {
                $out[] = $tree;
            }
        } else {
            $out[] = $new_tree;
        }
    }
    return $out;
}

$tree = flattenTree($tree);
print_r($tree);

Output:

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
        )

    [1] => Array
        (
            [0] => A
            [1] => C
            [2] => D
        )

    [2] => Array
        (
            [0] => E
        )

)
0
0

Another solution, without recursion (test please)

$result = array();

    foreach($category as $key => $value) {

        /*Get current row index*/
        $i = count($result);

        if($i == 0) {
            $result[] = array($key);
        } else {

            $iParent = -1;

            /*Find parent index*/
            for($j = count($result[$i-1]) - 1; $j >= 0; $j--) {
                if($value['left'] > $category[$result[$i-1][$j]]['left'] 
                    && $value['right'] < $category[$result[$i-1][$j]]['right']) {
                    $iParent = $j;
                    break;
                }
            }

            if($iParent == -1) { $result[] = array($key);}

            if($iParent == count($result[$i-1]) - 1) {
                // append to last
                $result[$i-1][] = $key;
            } else {
                // make new list
                $result[$i] = array_slice($result[$i-1], 0, $iParent + 1);
                $result[$i][] = $key;
            }
        }
    }

    print_r($result);
0

There is a bug with above function. The top category of second array of the @tree is removed. This is the fix:

foreach ($category as $name => $range) {
    $line[$range['left']] = $name;
    $line[$range['right']] = $name;
}

ksort($line);
$tree = array();
$count = 0;

foreach ($line as $name) {
    if (!isset($open[$name])) {
        $open[$name] = true;
        $count++;
    }
    else {
        if ($count > 0) {
            $count = 0;
            $tree[] = array_keys($open);
        }
        unset($open[$name]);
    }
}
0

I little modified Stiven's code.

public function createTree($category, $left = 0, $right = null) {
    $tree = array();
    foreach ($category as $cat => $range) {
        if ($range['clf'] == $left + 1 && (is_null($right) || $range['crt'] < $right)) {
            $tree[$cat]= array();
            $tree[$cat]['title']=$range['title'];
            if($range['crt']-$range['clf']>1){
                $tree[$cat]['sub'] = $this->createTree($category, $range['clf'], $range['crt']);
            }
            $left = $range['crt'];
        }
    }
    return $tree;
}
-1

If you dont want to use recursion:

foreach ($category as $name => $range) {
    $line[$range['left']] = $name;
    $line[$range['right']] = $name;
}

ksort($line);
$count = 0;

foreach($line as $name) {
    if ( ! isset($open[$name])) {
        $open[$name] = true;
        $result[$name] = true;
        $count++;
    } else {
        unset($open[$name]);
        if ($count > 0) {
            $count = 0;
            $tree[] = array_keys($result);
            $result = $open;
        } else {
            $result = array();
        }
    }
}
1
  • Recursion is necessary to handle potentially infinite levels of nesting. Did you test this? Jun 8, 2013 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.