由于大小不变,请存储它:
$size = sizeof($customers_table);
for ($i = 0; $i < $size; $i++) {
echo '<option value="'.$customers_table[$i]['id'].'">'.$customers_table[$i]['email'].'</option>';
}
然后您可以缩短循环条件并减少数组访问:
$i = $size = sizeof($customers_table);
while ($i) {
$table = $customers_table[$size-$i--];
echo '<option value="'.$table['id'].'">'.$table['email'].'</option>';
}
下一部分是 echo 语句,它可以在输出之前输出而不是连接字符串:
$i = $size = sizeof($customers_table);
while ($i) {
$table = $customers_table[$size-$i--];
echo '<option value="', $table['id'], '">', $table['email'], '</option>';
}
如果这有所作为,您需要进行度量。花费的最大时间可能是您发送到浏览器的 HTML 数量。但这超出了本片段的范围。
为了提高代码的整体可读性,我建议使用foreach
:
foreach ($customer_table as $row)
{
echo '<option value="', $table['id'], '">', $table['email'], '</option>';
}
这通常也很快。