note 38141 added to control-structures.foreach

From: Date: Tue, 09 Dec 2003 20:44:25 +0000
Subject: note 38141 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-61704@lists.php.net to get a copy of this message
If you ever find yourself wanting to create a table based on the contents of a multi-dimensional array, this: $output = ""; foreach ($ary AS $val) { $output .= " <tr>\n" ." <td>"; $output .= implode($val, "</td>\n <td>"); $output .= "</td>\n" ." </tr>\n"; } echo $output; unset ($output); is much faster than this: $output = ""; foreach ($ary AS $val) { $output .= " <tr>\n"; foreach ($val AS $val2) { $output .= " <td>$val2</td>\n"; } $output .= " </tr>\n"; } echo $output; unset ($output); Also note that I am compiling it in a variable before echoing it. I found this was faster than echoing each line seperately. ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38141 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38141&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38141&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#61704) next »