问题
I have been trying to solve this for while but its been a little difficult. the thing is, am trying nest the data from the table bellow into an array in the order by which they are related.
+-----+------+------------+
| uid | name | supermember|
+-----+------+------------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 3 |
| 7 | G | 3 |
| 9 | H | 4 |
| 10 | I | 4 |
| 11 | J | 7 |
+-----+------+------------+
Here is what i have done:
public function getDataAsBinaryTree($id)
{
$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;
}
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 > 1) ? $this->tolevel($key->uid) : $this->tolevel($key->uid));
}
}
return $output;
}
EXPECTED OUTPUT
array(
'A' => array(
'B' => array(
'D' => array(
'H' => 'H',
'I' => 'I'
)
),
'C' => array(
'E' => null,
'G' => array(
'J' => 'J'
)
)
)
);
i want the returned data from these methods to be in key and value pair but it instead returned an array with only keys when i var_dump on the getDataAsBinaryTree(1)
method. I think the problem is from the recursion but i just don't know how to get around it.
RESULT
array (size=1)
'A' =>
array (size=2)
'B' =>
array (size=1)
'D' =>
array (size=2)
...
'C' =>
array (size=2)
'E' =>
array (size=0)
...
'G' =>
array (size=1)
...
回答1:
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;
}
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. :)
来源:https://stackoverflow.com/questions/61545812/nesting-hierarchical-data-into-an-array-php-codeigniter