dongzheng4556 2015-07-31 10:40
浏览 46

php将数组转换为数据库的分层嵌套集

So I read this: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

And i'm using mysql and php What I need is script that convert only for one time the old data into data with the left/right values.

I don't need to add/update things.

The php data

$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,
                                            )
                                  )
                          )
                    )
            )   
);

At the moment i'm trying to solve this with this script but that don't work like it should.

$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);

I can access mysql and query so the output is like this

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

With that data I made the array above.

  • 写回答

1条回答 默认 最新

  • duanqiao1947 2015-07-31 12:30
    关注

    The array was not oke.

    This is working

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?