I have this kind of adjacency list:
I need to sort it to this: 2, 7, 9, 10, 8, 16, 17, 11
Basiccaly I have table where I have stored posts with parent ids. I can have unlimited number of levels.
I need just algorithm or full code in PHP.
I tried to use variations of row/column prefixes and some recurrent functions.
Okay, i finally did it. I was idi*t.
public function makeTree($parent_id = 0, &$array = array())
{
// Get results where parent_id == $parent_id
$recepts = $this->recept->get()->where("parent_id =" . $parent_id);
// Foreach through them
foreach ($recepts as $recept) {
// Add this to array
array_push($array, $recept);
// Repeat
$this->makeTree($recept["id"], $array);
}
return $array;
}