所以我读到了这个: 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
根据这些数据,我在上面制作了数组。
答案 0 :(得分:0)
阵列不是oke。
这是有效的
$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);