private function find_children ($parent_id, $children, &$result)
{
foreach ($children as $c)
{
if ($c->parent_comment_id == $parent_id)
{
$result[] = $c;
$this->find_children($c->id, $children, $result);
}
}
return;
}
The above function is supposed to take a starting parent id and recursively go through an array of children nodes (really just an object with a unique id and a parent id) sorting them so that each node comes directly after it's parent (see below for sample data).
But for some reason, the function doesn't execute as I expect. I have the following data for testing.
id: 1 pid: 0 (the initial parent which is not in the children array passed to func. problem?)
id: 2 pid: 1
id: 3 pid: 2
id: 4 pid: 1
id: 5 pid: 3
id: 6 pid: 5
id: 7 pid: 4
id: 8 pid: 3
and want the following array returned: 1, 4, 7, 2, 3, 8, 5, 6
But instead, I get: 1, 2, 3, 5, 6
which, while they're in the right order, a few are missing.
I have not had the need to do recursion in many years, so likely I am missing something obvious, although not so obvious to myself.
In case anyone is wondering, or if it matters, I'm trying to build a q&a commenting system where every post can have multiple replies.
Thus:
initial post
-reply to initial post #1
--reply to reply
-reply to initial post #2
-- reply to above
--- reply to above
--reply to #2