从php数组打印一个html表

时间:2016-05-27 09:15:28

标签: php html arrays

我需要打印一个表,从PHP数组开始,添加一些列以供进一步使用。

阵列是这样的:

Array ( [0] => Array ( [tid] => 1 [token] => andrea [participant_info] => Array ( [firstname] => Andrea [lastname] => AndreaLastName [email] => andrea@email.com ) ) [1] => Array ( [tid] => 3 [token] => 1 [participant_info] => Array ( [firstname] => 1FirstName [lastname] => 1LastName [email] => 1@email.com ) ) [2] => Array ( [tid] => 4 [token] => 2 [participant_info] => Array ( [firstname] => 2FirstName [lastname] => 2LastName [email] => 2@email.com ) ) [3] => Array ( [tid] => 5 [token] => 3 [participant_info] => Array ( [firstname] => 3FirstName [lastname] => 3LastName [email] => 3@email.com ) ) [4] => Array ( [tid] => 6 [token] => 4 [participant_info] => Array ( [firstname] => 4FirstName [lastname] => 4LastName [email] => 4@email.com ) ) [5] => Array ( [tid] => 7 [token] => 5 [participant_info] => Array ( [firstname] => 5FirstName [lastname] => 5LastName [email] => 5@email.com ) ) [6] => Array ( [tid] => 8 [token] => 6 [participant_info] => Array ( [firstname] => 6FirstName [lastname] => 6LastName [email] => 6@email.com ) ) [7] => Array ( [tid] => 9 [token] => 7 [participant_info] => Array ( [firstname] => 7FirstName [lastname] => 7LastName [email] => 7@email.com ) ) [8] => Array ( [tid] => 10 [token] => test [participant_info] => Array ( [firstname] => testFirstName [lastname] => testLastName [email] => test@email.com ) ) [9] => Array ( [tid] => 11 [token] => test3 [participant_info] => Array ( [firstname] => firstnameTest [lastname] => lastnameTest [email] => test2@email.com ) ) ) 

它看起来像使用一些在线工具: Array 我需要的是创建一个HTML表格(使用经典< table>< div>不关心)

看起来像这样: enter image description here 令牌没有直接打印,但它可用作$variable用于某些脚本(我需要添加图标以下载名为&#34; $ token.pdf&#34;的文件)

感谢您的任何建议。

我找到了一些直接打印数组的函数,但我不知道如何适应我的需求:

function build_table($array){
    // start table
    $html = '<table border="1" style="width:100%">';
    // header row
    $html .= '<tr>';
    foreach($array[0] as $key=>$value){
        $html .= '<th>' . $key . '</th>';
    }
    $html .= '</tr>';

    // data rows
    foreach( $array as $key=>$value){
        $html .= '<tr>';
            foreach($value as $key2=>$value2){
                $html .= '<td>' . $value2 . '</td>';
            }
        $html .= '</tr>';
    }

    // finish table and return it

    $html .= '</table>';
    return $html;
} 

1 个答案:

答案 0 :(得分:0)

我重新制作了数组以提供一个很好的例子。

如你所见,我使用2个foreach循环。 第一个是通过主数组'$ array'循环。 第一个foreach中的第二个foreach从'participant_info'循环遍历数组。

$ i代表数组中的值。 所以如果你输入:$ i ['did'],它会循环来自'tid'的所有值。

我希望这可以解释它。

祝你好运!

<?php

    // Made array
    $array[0] = array("tid" => 1, "token" => "andrea", "participant_info" => array("firstname" => "Andrea", "lastname" => "AndreaLastName", "email" => "andrea@email.com" ));
    $array[1] = array("tid" => 3, "token" => 1, "participant_info" => array("firstname" => "1FirstName", "lastname" => "1LastName", "email" => "1@email.com" ));
    $array[2] = array("tid" => 4, "token" => 2, "participant_info" => array("firstname" => "2FirstName", "lastname" => "2LastName", "email" => "2@email.com" ));
    $array[3] = array("tid" => 5, "token" => 3, "participant_info" => array("firstname" => "3FirstName", "lastname" => "3LastName", "email" => "3@email.com" ));
    $array[4] = array("tid" => 6, "token" => 4, "participant_info" => array("firstname" => "4FirstName", "lastname" => "4LastName", "email" => "4@email.com" ));
    $array[5] = array("tid" => 7, "token" => 5, "participant_info" => array("firstname" => "5FirstName", "lastname" => "5LastName", "email" => "5@email.com" ));
    $array[6] = array("tid" => 8, "token" => 6, "participant_info" => array("firstname" => "6FirstName", "lastname" => "6LastName", "email" => "6@email.com" ));
    $array[7] = array("tid" => 9, "token" => 7, "participant_info" => array("firstname" => "7FirstName", "lastname" => "7LastName", "email" => "7@email.com" ));
    $array[8] = array("tid" => 10, "token" => "test", "participant_info" => array("firstname" => "testFirstName", "lastname" => "testLastName", "email" => "test@email.com" ));
    $array[9] = array("tid" => 11, "token" => "test3", "participant_info" => array("firstname" => "firstnameTest", "lastname" => "lastnameTest", "email" => "test2@email.com" ));

    ?>


    <table>
    <tr>
        <th>tid</th>
        <th>token</th>
        <th>participant_info</th>
        <th>firstname</th>
        <th>lastname</th>
        <th>email</th>
    </tr>

    <? foreach($array as $h => $i): ?>
    <tr>
        <td><?=$i['tid']?></td>
        <td><?=$i['token']?></td>
            <? foreach($i['participant_info'] as $p): ?>
                <td><?=$p?></td>
            <? endforeach; ?>
    </tr>
    <? endforeach; ?>
    </table>