![](/img/trans.png)
[英]How to create an associative array from more than two columns fetched from a sql query in PHP?
[英]PHP array to table with more than two columns
我有一些代码将两个数组组合成一个有 2 列的表。 这有效。 但是现在我有一个包含$file3
值的新数组。 我了解如何制作包含两列的表格,但如何添加包含$file3
内容的第三列?
<?php
$file1 = "c:/presencetool/ramfile1.txt";
$file2 = "c:/presencetool/ramfile2.txt";
$file3 = "c:/presencetool/ramfile3.txt"; //new file
if(file_exists($file1) && file_exists($file2))
{
$line1 = file($file1, FILE_IGNORE_NEW_LINES);
$line2 = file($file2, FILE_IGNORE_NEW_LINES);
$line3 = file($file3, FILE_IGNORE_NEW_LINES); //new array
$combine = array_combine($line1, $line2); //I want to also combine $line3 here
$html = '<table align="center">';
$html .= '<tr><td width="350px";></td><td></td></tr>';
$i = 1;
foreach ($combine as $key => $value):
$html .= '<tr class="'.$value.'">';
$html .= '<td font-size:"90pt">'.$key.'</td>';
$html .= '<td font-size:"90pt">'.$value.'</td>';
//here something like $html .= '<td font-size:"90pt">'.$value2.'</td>';
$html .= '</tr>';
$i++;
endforeach;
$html .= '</table>';
echo $html;
}
在$file1
, $file2
和$file3
是一些用户的姓名、地址和电子邮件。(每行一个值)
如果您的数组如下所示:
$line[] = ['name' => 'name1','email' => 'email@mail.com'];
$line[] = ['name' => 'name2','email' => 'email2@mail.com'];
$line[] = ['name' => 'name3','email' => 'email3@mail.com'];
然后试试下面的,希望能帮到你
$html = '<table align="center" border="1" width="50%">';
$html .= '<tr><th>Name</th><th>Email</th><th>Address</th></tr>';
for($i=0;$i<count($line1);$i++){
$html .= '<tr class="'.$line1[$i].'">';
$html .= '<td font-size:"90pt">'.$line1[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line2[$i].'</td>';
$html .= '<td font-size:"90pt">'.$line3[$i].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.