更新时间:2022-11-22 15:29:08
老实说,我不完全理解您的问题,您应该提供输出代码的结构.但是正如您的函数 getDataAsBinaryTree
所建议的那样,您正在尝试创建一个二叉树,如果是这种情况,这就是要走的路.
Honestly, I don't understand your question completely, you should provide the structure of output code. But as your function getDataAsBinaryTree
suggests you're trying to make a binary tree, if that's the case then this is the way to go.
public function getDataAsBinaryTree($id = false){
// $this->db->where('uid', $id);
$query = $this->db->get('binary_tbl');
$result = [];
if ($query->num_rows() > 0) {
foreach ($query->result() as $k) {
$result[$k->name] = $this->tolevels($k->uid);
}
} else {
$result = NULL;
}
// return $result;
echo '<pre>'; print_r($result);
}
private function tolevels($id){
$this->db->where('supermember', $id);
$query = $this->db->get('binary_tbl');
$output = [];
$count = $query->num_rows();
if ($query->num_rows() > 0) {
foreach ($query->result() as $key) {
// $output[$key->name] = ($count > 0) ? $this->tolevels($key->uid) : $key->uid;
$output[$key->name] = $key->uid;
if($count > 0){ $this->tolevels($key->uid); };
}
}
return $output;
}
输出
Array
(
[A] => Array
(
[B] => 2
[C] => 3
)
[B] => Array
(
[D] => 4
)
[C] => Array
(
[E] => 5
[G] => 6
)
[D] => Array
(
[H] => 7
[I] => 8
)
[E] => Array
(
)
[G] => Array
(
)
[H] => Array
(
[J] => 9
)
[I] => Array
(
)
[J] => Array
(
)
)
希望,这对您有帮助.:)
Hope, this helps you. :)