简体   繁体   English

将多级关联数组显示为表

[英]Display Multilevel Associative array as table

The following is an example of an array that I would like to display: 以下是我要显示的数组的示例:

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
        )
    )
)

So as you can see it has multiple teams and each team has multiple dates that they meet and on each day there will different people with different jobs. 因此,您可以看到它有多个团队,每个团队都有多个约会的日期,每一天都有不同的人从事不同的工作。 Using this table I would like for it to display as such: 我希望使用此表将其显示为:

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

I've tried using foreach statements, but it just gets to be too much as soon as I get further. 我已经尝试过使用foreach语句,但是一旦深入,它就会变得太多。 Any help is very much appreciated. 很感谢任何形式的帮助。

Here you are: 这个给你:

<?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]

If you want separate tables for each team, you can put the into the loop. 如果要为每个团队使用单独的表,则可以将其放入循环中。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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