note 38141 modified in control-structures.foreach by vrana
| From: | vrana@php.net | Date: | Thu, 05 Aug 2004 18:11:37 +0000 |
| Subject: | note 38141 modified in control-structures.foreach by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74045@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:
<?php
$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:
<?php
$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.
--was--
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.
http://php.net/manual/en/control-structures.foreach.php