I'm working on a comment system, in which one is able to reply to another comment, making it appear below the commented comment. I have a bunch of parent/child pairs, to which end I use recursion to convert this into a tree structure:
private function _create_tree_from_comments($comment_id, $entry_id) {
$node_set = array();
$children = $this->comment_model->get_children($comment_id, $entry_id);
// check if we're a leaf
if($children->num_rows() < 1) {
return $comment_id;
}
foreach($children->result() as $child) {
$node_set[$child->child] = $this->_create_tree_from_comments($child->child, $entry_id);
}
return $node_set;
}
Now this is all fine and well, but the problem is that I need to save information about intermediate nodes. Currently all I can do is save whatever information I need in another array at a leaf, since the intermediate nodes have responsibility to keep information about their children.
So my question is really: How am I able to save information about intermediate nodes without breaking the relationship there is between the parent and its children.
Some sample output of the function:
Array
(
[5] => 5
[2] => 2
[1] => Array
(
[4] => Array
(
[6] => 6
)
[3] => 3
)
)