I have a table that consists of comments. Some of them are replies to other comments and have a value set in parent_commentid table. I'm trying to create a function that checks each element in a result set if there is a value in the parent_columnid and if so take the entire element and sort it inside the element with a comment_id that matches the parent_commentid of the current element in the iteration. This is what I've come up with so far.
function sort_comments($comments){
$result = array();
foreach($comments as $comment){
if(is_null($comment['parent_commentid'])) $result[] = $comment;
else{
$parent_comment = array_search($comment['parent_commentid'], $comments);
if($parent_array !== false) $result[$parent_comment][] = $comment;
}
}
}
array_search is not the function I'm looking for but is the closets thing I could think of. Im not sure where to go from here. Keep in mind also that there can exist replies to other replies.