我想得到"得分"在所有分支的根目录。我的代码管理得到所有正确但不在第一级。 我已经尝试了所有我认识的工作 - 没有运气。
我有以下代码:
查询数据库:
$db->query("SELECT * FROM EvalSections WHERE EvalKitID = ".CCGetFromGet('Kit'));
while($db->next_record()){
$data[] = array ('id' => $db->f("IDSection"),
'name' => $db->f("SectionName"),
'parent' => $db->f("SectionParentID"),
'depth' => 0,
'score' => 0,
'sort' => $db->f("SectionSort")
);
}
构建数组:
function buildTree(array $elements, $parentId = 0,$depth = 1) {
$branch = array();
global $TotalGeneral;
foreach ($elements as $element) {
if ($element['parent'] == $parentId) {
$children = buildTree($elements, $element['id'],$depth+1);
if ($children) {
$TotalGeneral = 0;
usort($children, function($a, $b) {
return $a['sort'] - $b['sort'];
});
foreach($children as $c => $v){
$Total = getScore($v['id']);
$TotalGeneral += $Total;
$children[$c]['depth'] = $depth+1;
$children[$c]['score'] += $Total;
}
$Total = getScore($element['id']);
$TotalGeneral += $Total;
$element['depth'] = $depth;
$element['score'] += $TotalGeneral;
$element['children'] = $children;
}else{
$Total = getScore($element['id']);
$TotalGeneral += $Total;
//$element['score'] += $Total;
}
//$element['score'] = $TotalGeneral;
$branch[] = $element;
}
}
return $branch;}
在print_r上我得到:
Array
(
[0] => Array
(
[id] => 196
[name] => Abilități spirituale
[parent] => 0
[depth] => 1
[score] => 152 = WRONG VALUE
[sort] =>
[children] => Array
(
[0] => Array
(
[id] => 395
[name] => Date generale
[parent] => 196
[depth] => 2
[score] => 0
[sort] => 1
)
[1] => Array
(
[id] => 197
[name] => Armonia cu sine
[parent] => 196
[depth] => 2
[score] => 145
[sort] => 2
[children] => Array
(
[0] => Array
(
[id] => 201
[name] => Gândirea pozitivă
[parent] => 197
[depth] => 3
[score] => 57
[sort] => 1
)
[1] => Array
(
[id] => 202
[name] => Satisfacția personală
[parent] => 197
[depth] => 3
[score] => 38
[sort] => 2
)
[2] => Array
(
[id] => 203
[name] => Concordanța cu sine
[parent] => 197
[depth] => 3
[score] => 29
[sort] => 3
)
[3] => Array
(
[id] => 204
[name] => Managementul calitativ al timpului liber
[parent] => 197
[depth] => 3
[score] => 21
[sort] => 4
)
)
)
[2] => Array
(
[id] => 198
[name] => Armonia cu ceilalți
[parent] => 196
[depth] => 2
[score] => 54
[sort] => 3
)
[3] => Array
(
[id] => 199
[name] => Sensul vieții și misiunea personală
[parent] => 196
[depth] => 2
[score] => 49
[sort] => 4
)
[4] => Array
(
[id] => 200
[name] => Perspectiva de ansamblu asupra realității
[parent] => 196
[depth] => 2
[score] => 49
[sort] => 5
)
)
)
)