【问题标题】:Build table with rowspan using multidimensional array使用多维数组构建具有行跨度的表
【发布时间】:2018-12-21 07:08:01
【问题描述】:

我希望以日历格式显示一周的学生和活动。我有这个数组,如下所示。每个学生从周一到周日都有 7 个数组,每个学生的内部数组都有一天的事件

$array = [
    'Alex' => [
        [
            ['event' => 'eventName1'],['event' => 'eventName2']
        ],
        [
            ['event' => 'eventName3'],['event' => 'eventName4']
        ],
        [
            ['event' => 'eventName5'],['event' => 'eventName6']
        ],
        [
            ['event' => 'eventName7'],['event' => 'eventName8']
        ],
        [],
        [],
        []

    ], 
    'Allen' => [
        [
            ['event' => 'eventName'],['event' => 'eventName']   
        ],[
            ['event' => 'eventName'],['event' => 'eventName']  
        ],
        [],
        [],
        [],
        [],
        []
    ],
];

我需要将数组显示为周历, 我不知道下面的代码哪里出错了

<table>
    <thead>
        <tr>
            <th>Participant</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thrusday</th>
            <th>Friday</th>
            <th>Satday</th>
            <th>Sunday</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $participant => $event): ?>
        <?php foreach ($event as $i => $value):?>
        <tr>
            <?php if ($i === 0): ?>
            <td rowspan="2"><?= $participant ?></td>
            <?php endif ?>
            <?php foreach ($value as $index  => $eventD):?>

            <td><?php echo $eventD['event']; ?></td>

        </tr>
        <?php endforeach ?>
        <?php endforeach ?>
        <?php endforeach ?>
    </tbody>
</table>

我的问题是循环以及如何打印特定日期的事件。 任何人都可以建立正确的格式。感谢帮助

【问题讨论】:

    标签: php mysql multidimensional-array


    【解决方案1】:
    <?php
    
    $array = [
        'Alex' => [
            [
                ['event' => 'eventName1'],['event' => 'eventName2']
            ],
            [
                ['event' => 'eventName3'],['event' => 'eventName4']
            ],
            [
                ['event' => 'eventName5'],['event' => 'eventName6']
            ],
            [
                ['event' => 'eventName7'],['event' => 'eventName8']
            ],
            [],
            [],
            []
        ], 
        'Allen' => [
            [
                ['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],   
            ],[
                ['event' => 'eventName'],['event' => 'eventName']  
            ],
            [],
            [],
            [],
            [],
            []
        ],
    ];
    
    $maxRows = [];
    foreach ($array as $participant => $weekEvents) {
        $max = 0;
        foreach ($weekEvents as $dayEvents) {
            if (count($dayEvents) > $max) {
                $max = count($dayEvents);
            }
        }
    
        $maxRows[$participant] = $max;
    }
    
    ?>
    
    <table border="1">
        <thead>
            <tr>
                <th>Participant</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thrusday</th>
                <th>Friday</th>
                <th>Satday</th>
                <th>Sunday</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($array as $participant => $weeklyEvents): ?>
                <?php for ($i = 0; $i < $maxRows[$participant]; $i++): ?>
                    <tr>
                        <?php if ($i === 0): ?>
                            <td rowspan="<?php echo $maxRows[$participant]; ?>">
                                <?php echo $participant; ?>
                            </td>
                        <?php endif; ?>
    
                        <?php for ($j = 0; $j < 7; $j++): ?>
                            <td>
                                <?php $dayEvents = $weeklyEvents[$j]; ?>
    
                                <?php if (isset($dayEvents[$i])): ?>
                                    <?php echo $dayEvents[$i]['event']; ?>
                                <?php else: ?>
                                    &nbsp;
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                <?php endfor; ?>
            <?php endforeach; ?>
        </tbody>
    </table>
    

    【讨论】:

      【解决方案2】:

      检查这个html

      <table border="1">
          <thead>
              <tr>
                  <th>Participant</th>
                  <th>Monday</th>
                  <th>Tuesday</th>
                  <th>Wednesday</th>
                  <th>Thrusday</th>
                  <th>Friday</th>
                  <th>Satday</th>
                  <th>Sunday</th>
              </tr>
          </thead>
          <tbody>
              <?php foreach ($array as $participant => $event): ?>
                  <tr>
                      <td><?= $participant ?></td>
                      <?php foreach ($event as $i => $value): ?>
                          <td>
                              <?php foreach ($value as $index => $eventD): ?>
                                  <?php echo $eventD['event'] . '<br/>'; ?>
                              <?php endforeach ?>
                          </td>
                      <?php endforeach ?>
                  </tr>
              <?php endforeach ?>
          </tbody>
      </table>
      

      【讨论】:

        【解决方案3】:

        如果我的理解是正确的试试这个

        <?php
        
          $array = [
        'Alex' => [
            [
                ['event' => 'eventName1'],['event' => 'eventName2']
            ],
            [
                ['event' => 'eventName3'],['event' => 'eventName4']
            ],
            [
                ['event' => 'eventName5'],['event' => 'eventName6']
            ],
            [
                ['event' => 'eventName7'],['event' => 'eventName8']
            ],
            [],
            [],
            []
        
         ], 
        'Allen' => [
            [
                ['event' => 'eventName'],['event' => 'eventName']   
            ],[
                ['event' => 'eventName'],['event' => 'eventName']  
            ],
            [],
            [],
            [],
            [],
            []
           ],
        ];
        
            echo "<table border='1'>
         <thead>
            <tr>
                <th>Participant</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thrusday</th>
                <th>Friday</th>
                <th>Satday</th>
                <th>Sunday</th>
            </tr>
        </thead>
        <tbody>";
             foreach($array as $key => $data)
              {
            echo "<tr>
            <td>$key</td>";
            foreach($data as $data2) {
                if(is_array($data2) && isset($data2[0])) {
                    echo "<td>";
                     array_walk($data2, function($val, &$dat){
                        echo $val['event'].', ';
                    });
                    echo "</td>";
                } else {
                    echo "<td></td>";
                }
            }
            echo "</tr>";
        }
           echo "</tbody>
            </table>";
             ?>
        

        小提琴链接http://sandbox.onlinephpfunctions.com/

        【讨论】:

          猜你喜欢
          • 2014-04-15
          • 2019-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-19
          • 2015-05-18
          相关资源
          最近更新 更多