我试图在使用父数据库从数据库中填充的族谱树中获取特定父数据库的子数据的总计数。
我已经尝试了一些代码,但结果采用的是二进制形式。
function getChildren($parent) {
global $connection;
$query = "SELECT * FROM member_log WHERE parent_id = $parent";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$children = array();
while ($row = mysqli_fetch_assoc($result)) {
$children[$row['id']]['username'] = $row['username'];
$children[$row['id']]['children'] = getChildren($row['id']);
}
return $children;
}
$user_id = "100";
$finalResult = getChildren($user_id);
$final = count($finalResult);
function printList($array = null) {
if (count($array)) {
foreach($array as $item) {
$e = 1;
echo $e;
if (count($item['children'])) {
printList($item['children']);
$a = count($item['children']);
}
}
}
}
echo printList($finalResult);
输出:111111
预期产量:6
来自的输出var_dump($finalresult);是:
array(3) {
[101]=> array(2) {
["username"]=> string(8) "1st user"
["children"]=> array(3) {
[104]=> array(2) {
["username"]=> string(8) "4th user"
["children"]=> array(0) { }
}
[105]=> array(2) {
["username"]=> string(8) "5th user"
["children"]=> array(0) { }
}
[108]=> array(2) {
["username"]=> string(7) "new guy"
["children"]=> array(0) { }
}
}
}
[102]=> array(2) {
["username"]=> string(8) "2nd user"
["children"]=> array(0) { }
}
[103]=> array(2) {
["username"]=> string(8) "3rd user"
["children"]=> array(0) { }
}
}