php convert array into a hierarchical nested set for database

送分小仙女□ 提交于 2019-12-11 05:48:27

问题


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:


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


来源:https://stackoverflow.com/questions/31744191/php-convert-array-into-a-hierarchical-nested-set-for-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!