Mysql
 sql >> Database >  >> RDS >> Mysql

php converte l'array in un set nidificato gerarchico per il database

L'array non era a posto.

Funziona

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