The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled PHP Code
Posted by: Luke | March 14, 2011 @ 9:15pm
PHP Code
[
Download
]
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--) { echo "\t"; } echo $comment['text']; echo "\n"; } /** * @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++) { $start = microtime(true); $threaded_comments = new Threaded_comments($comments); $threaded_comments->print_comments(); $end = microtime(true); $sum += ($end-$start); } $average = $sum / 20000;
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.55 / 8
66 Votes
http://codebin.yi.org/1167
page generated in 0.01 seconds