class Threaded_comments
{
public $children =
array();
public $top;
/**
* @param array $comments
*/
function __construct($comments)
{
foreach ($comments as $comment)
{
$this->children[$comment['parent_id']][] = $comment;
}
}
/**
* @param array $comment
* @param int $depth
*/
private function format_comment($comment, $depth)
{
for ($depth; $depth > 0; $depth--)
{
}
}
/**
* @param array $comment
* @param int $depth
*/
private function print_parent($comment, $depth = 0)
{
foreach ($comment as $c)
{
$this->format_comment($c, $depth);
if (isset($this->
children[$c['id']])) {
$this->print_parent($this->children[$c['id']], $depth + 1);
}
}
}
public function print_comments()
{
foreach ($this->children[0] as $c)
{
if (isset($this->
children[$c['id']])) {
$this->print_parent($this->children[$c['id']]);
}
}
}
}
$comments =
array(array('id'=>
1,
'parent_id'=>
0,
'text'=>
'top'),
array('id'=>
2,
'parent_id'=>
1,
'text'=>
'Parent'),
array('id'=>
3,
'parent_id'=>
2,
'text'=>
'Child'),
array('id'=>
7,
'parent_id'=>
2,
'text'=>
'Child'),
array('id'=>
4,
'parent_id'=>
3,
'text'=>
'Child Third level'),
array('id'=>
5,
'parent_id'=>
1,
'text'=>
'Second Parent'),
array('id'=>
6,
'parent_id'=>
5,
'text'=>
'Second Child') );
for ($foo=0;$foo<20000;$foo++)
{
$threaded_comments = new Threaded_comments($comments);
$threaded_comments->print_comments();
$sum += ($end-$start);
}
$average = $sum / 20000;