How do I format Nested Set Model data into an array?

前端 未结 5 1383
长发绾君心
长发绾君心 2020-12-09 14:18

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

$category = array(
  \'A\' => array(\'left\' => 1, \'right\' => 8),
  \'B\' =&         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 14:46

    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]);
        }
    }
    

提交回复
热议问题