【问题标题】:php convert array into a hierarchical nested set for databasephp将数组转换为数据库的分层嵌套集
【发布时间】:2015-10-23 00:23:02
【问题描述】:

所以我读到了这个: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

我正在使用 mysql 和 php 我需要的是只将旧数据转换为具有左/右值的数据的脚本。

我不需要添加/更新东西。

php数据

$in_array = array (
         array(
                'id' => 400,
                'n' => 'Sub 1a',
                's' => array (
                         array (
                                'n' => 'Sub 1b',
                                'id' => 421,
                                's' => array (
                                         array (
                                                'n' => 'Sub 1c',
                                                'id' => 422,
                                            )
                                  )
                          )
                    )
            ),
         array(
                'id' => 500,
                'n' => 'Sub 2a',
                's' => array (
                        array (
                                'n' => 'Sub 2b',
                                'id' => 521,
                                's' => array (
                                        array (
                                                'n' => 'Sub 3b',
                                                'id' => 522,
                                            )
                                  )
                          )
                    )
            )   
);

目前我正在尝试用这个脚本解决这个问题,但它不能正常工作。

$a_newTree = array();
function rebuild_tree($parent, $left) {   
global $a_newTree;

$indexed = array();

// the right value of this node is the left value + 1   
$right = $left+1;   

// get all children of this node   
foreach($parent as $cat){
        // recursive execution of this function for each   
        // child of this node   
        // $right is the current right value, which is   
        // incremented by the rebuild_tree function

        if(is_array($cat) && array_key_exists('s',$cat)){
            $indexed['n'] = $cat['n'];
            $right = rebuild_tree($cat, $right);
        }
}   

// we've got the left value, and now that we've processed   
// the children of this node we also know the right value   
array_push($a_newTree,array(':lft'=>$left,':rgt'=>$right,':name'=>'bla'));
// return the right value of this node + 1   
return $right+1;   
}   

rebuild_tree($in_array, 1);

我可以访问mysql并查询所以输出是这样的

Sub 1a | Sub 1b | Sub 1c

Sub 1a | Sub 1b | Sub 1d

Sub 1a | Sub 1b | Sub 1e

Sub 1a | Sub 1f | Null

Sub 1a | Sub 1g | Null

Sub 2a | Sub 2b | Sub 2c

我用这些数据制作了上面的数组。

【问题讨论】:

    标签: php mysql dataset hierarchical


    【解决方案1】:

    数组不正确。

    这是有效的

    $in_array = array (
        // array(
                'id' => 400,
                'n' => 'Sub 1a',
                's' => array (
                //       array (
                                'n' => 'Sub 1b',
                                'id' => 421,
                                's' => array (
                        //               array (
                                                'n' => 'Sub 1c',
                                                'id' => 422,
                            //              )
                                  )
                    //    )
            //      )
            ),
         array(
                'id' => 500,
                'n' => 'Sub 2a',
                's' => array (
                    //  array (
                                'n' => 'Sub 2b',
                                'id' => 521,
                                's' => array (
                            //          array (
                                                'n' => 'Sub 3b',
                                                'id' => 522,
                                //          )
                                  )
                        //  )
                    )
            )   
    );
    
    $a_newTree = array();
    function rebuild_tree($parent, $left) {   
    global $a_newTree;
    
    $indexed = array();
    $right = $left+1;   
    
    if(array_key_exists('n',$parent)){
        $indexed['n'] = $parent['n'];
    }else{
        $indexed['n'] = '?';
    }
    
    
        foreach($parent as $cat){
                if(is_array($cat)){
                    $right = rebuild_tree($cat, $right);    
                }
        }
    
    
    array_push($a_newTree,array(':lft'=>$left,':rgt'=>$right,':name'=>$indexed['n']));
    
    return $right+1;   
    }   
    
    rebuild_tree($in_array, 1);
    

    【讨论】:

    • 这很有趣。哪里有深度?
    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    相关资源
    最近更新 更多