【发布时间】:2019-11-09 02:32:56
【问题描述】:
我有两个不同大小的数组,想在同一个表格中并排显示它们。
我尝试循环运行两个数组,但问题是较短的数组用完了索引。
这是我尝试过的:
$clean_s = ['apple','ball','cat','dog'];
$clean_r = ['apple','bat','carrot','duck','elephant','fan'];
if(sizeof($clean_s) == sizeof($clean_r)) {
$max = sizeof($clean_s);
} else {
$max = sizeof($clean_s) > sizeof($clean_r) ? sizeof($clean_s) : sizeof($clean_r);
}
$table = '<table><thead><tr><th>Source</th><th>Result</th></thead><tbody>';
for($i=0; $i < $max; $i++) {
$table .= '<tr><td>'.$clean_s[$i].'</td><td>'.$clean_r[$i].'</td></tr>';
}
需要输出:
Source | Result
________________________
apple | apple
ball | bat
cat | carrot
dog | duck
| elephant
| fan
【问题讨论】:
标签: php arrays for-loop html-table