简体   繁体   中英

PHP array to table with more than two columns

I have a little code that combines two arrays into a table with 2 columns. This works. But now I have a new array with values from $file3 . I understand how to make a table with two columns, but how can I add a third column with the content of $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;
    }

In $file1 , $file2 and $file3 are names, addresses and emails of some users.(one value per line)

if your array looks like this :

$line[] = ['name' => 'name1','email' => 'email@mail.com']; 
$line[] = ['name' => 'name2','email' => 'email2@mail.com']; 
$line[] = ['name' => 'name3','email' => 'email3@mail.com'];

Then try the below, hope will help you

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM