In users table I have following data.
id name parent_id
1 A 0
2 B 1
3 C 2
4 D 3
5 E 4
6 F 1
A have four level children. E is also children of A. E is the last, it has not any children. Now I can fetch all the children of any parent like the following code.
function getChildren($Id){
$cache = [];
$chList = [];
$allUser = User::orderBy('parent_id', 'asc')
->orderBy('parent_id', 'asc')
->get();
foreach($allUser as $user) {
$cache [$user->parent_id][] = $user;
}
$children0 = empty($cache [$Id]) ? [] : $cache [$Id];
#first level child
foreach($children0 as $child1) {
$chList[$child1->id] = $child1;
if(!empty($cache [$child1->id])) {
#Second level child
foreach($cache [$child1->id] as $child2) {
$chList[$child2->id] = $child2;
if(!empty($cache [$child2->id])) {
#third level child
foreach($cache [$child2->id] as $child3) {
$chList[$child3->id] = $child3;
if(!empty($cache [$child3->id])) {
#fourth level child
foreach($cache [$child3->id] as $child4) {
#can not be parent
$chList[$child4->id] = $child4;
}
}
}
}
}
}
}
return $chList;
}
I can get the actual result by this code. But for huge data it is time consuming. Can any one help me to reduce time complexity or optimize the code?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire