4

以下是我要显示的数组示例:

Array
(
    [Media] => Array
    (
        [2012-12-10] => Array
        (
            [Mentor] => Evan Tobin
            [Veteran Member] => James
        )

        [2012-12-21] => Array
        (
            [Mentor] => Evan Tobin
        )
    )
    [Website] => Array
    (
        [2012-12-10] => Array
        (
            [Mentor] => Evan Tobin
        )

        [2012-12-21] => Array
        (
            [Mentor] => Evan Tobin
        )
    )
)

所以你可以看到它有多个团队,每个团队有多个约会日期,每天都会有不同的人从事不同的工作。使用此表,我希望它显示为:

Media Team
Role           || 2012-12-10 || 2012-12-21
Mentor         || Evan Tobin || Evan Tobin
Veteran Member ||   James    || 

我尝试过使用 foreach 语句,但是一旦我走得更远,它就会变得太多。很感谢任何形式的帮助。

4

1 回答 1

2

这个给你:

<?php
// [your array]
$tabledata = array(
    'Media' => array(
        '2012-12-10' => array(
            'Mentor' => 'Evan Tobin',
            'Veteran Member' => 'James'
        ),
        '2012-12-21' => array(
            'Mentor' => 'Evan Tobin'
        )
    ),
    'Website' => array(
        '2012-12-10' => array(
            'Mentor' => 'Evan Tobin'
        ),
        '2012-12-21' => array(
            'Mentor' => 'Evan Tobin'
        )
    )
);
// [/your array]

// [the tables]
echo '<table border="1">';
foreach($tabledata as $teamkey => $teamval){
    // [helper]
    $dates = array();
    $roles = array();
    foreach($teamval as $datekey => $dateval) {
        if (!in_array($datekey, $dates)) {
            $dates[] = $datekey;
        }
        foreach($dateval as $rolekey=>$roleval) {
            if (!in_array($rolekey, $roles)) {
                $roles[] = $rolekey;
            }
        }
    }
    // [/helper]

    // [team name] >> row 1
    echo '<tr>';
    echo '<th align="left" colspan="'.(sizeof($dates)+1).'">'.$teamkey.' Team</th>';
    echo '</tr>';
    // [/team name]

    // [role column and date column] >> row 2
    echo '<tr>';
    echo '<td>Role</td>';
    foreach($dates as $date) {
        echo '<td>'.$date.'</td>';
    }
    echo '</tr>';
    // [/role column and date column]

    // [role and team member for each date] >> row 3, 4, 5, ... n
    foreach($roles as $role) {
        echo '<tr>';
        echo '<td>'.$role.'</td>';
        foreach($dates as $date) {
            echo '<td>';
            if (isset($teamval[$date][$role])) {
                echo $teamval[$date][$role]; // team member name
            }
            else {
                echo '&nbsp;'; // insert blank space for cross browser support
            }
            echo '</td>';
        }
        echo '</tr>';
    }
    // [/role and team member for each date]
}
echo '</table>';
// [/the tables]

如果您想为每个团队提供单独的表格,您可以将 放入循环中。

希望这可以帮助。

于 2012-12-17T01:09:58.693 回答