【问题标题】:php foreach combine 2 array into 1 row (workdays & data)php foreach 将 2 个数组组合成 1 行(工作日和数据)
【发布时间】:2017-04-19 08:39:54
【问题描述】:

我当前的“出勤项目”有问题,所以我有 2 个数组。

  • 第一个数组是显示“工作日” 第一个数组仅显示当前月份的工作日,例如:四月,所以我的第一个数组中的结果是 (3,4,5,6,7,10,11,12,13,14,17,18,19,20, 21,24,25,26,27,28)
  • 第二个数组显示当前月份 ex:April 的员工出勤率,所以我的第二个数组中的结果是 (17, 19)

这是我当前的代码:

<table class="table table-striped table-bordered zero-configuration">
    <thead>
        <tr>
            <th style="width: 200px">Siswa</th>
            <!-- <?php for($i = 1; $i < 31; ++$i){?>
            <th><?= $i ?></th>
            <?php } ?> -->
            <?php foreach($workdays as $w){ ?>
            <th><?=$w;?></th>
            <?php } ?>
        </tr>
    </thead>
    <tbody>
        <?php 
        // for($x = 1; $x < 27; ++$x){
        foreach($records as $r){
        ?>
        <tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
    ?>
    <?php 
         foreach($workdays as $w){
           foreach($tanggale as $t){ 

            if($w == $t){
            ?>
                <td style="background: #FFF000">M</td>
                <?php }else{ ?>
                <td style="background: #48C9A9">O</td>
                <?php } } } ?>
        </tr>
        <?php }  ?>
    </tbody>
</table>

它将产生:

我希望值(17 和 19)将用黄色背景标记数据,并且表格不会超出范围。 任何帮助将不胜感激..

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    您的代码看起来很乱,我不会尝试根据您所拥有的进行修复,但我会建议解决方案:

    1st - 运行 foreach ($workdays as $w) 并制作标题 2nd - 运行 foreach ($workdays as $w) 并使表体如下:

    foreach ($workdays as $w) {
        if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
        {
             //code
        }
        else
        {
             //code
        }
    }
    

    【讨论】:

    • 嵌套在 foreach 循环中的 foreach 循环一定是“超出范围”部分,因为它运行循环 * 循环次数
    【解决方案2】:

    您可以做的就是将 2 个数组合并为一个,然后根据您的要求迭代合并数组。 检查下面的代码以组合数组

    <?php
        $working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
        $present_days = array(17.19);
        $combine_attendence_array = array();
        foreach($working_days as $day) {
        $combine_attendence_array[$day] = 'Absent';
            if(in_array($day, $present_days)) {
                $combine_attendence_array[$day] = 'Present';
            }
        }
    ?>
    

    此代码将创建组合数组,其中键为日期,值存在或不存在。

    现在您可以根据您的要求进行迭代,下面是迭代代码。

    foreach($combine_attendence_array as $day => $value){
                if($value == 'Present'){  ?>
                    <td style="background: #FFF000">M</td>
                    <?php }else{ ?>
                    <td style="background: #48C9A9">O</td>
                    <?php }     ?>
                    <?php }    ?>   
    

    我希望这个答案能解决你的问题。

    【讨论】:

      【解决方案3】:

      这样做

      <?php 
              foreach($tanggale as $t){ 
                  
                  if(in_array($t,$workdays)){
                  ?>
                      <td style="background: #FFF000">M</td>
                      <?php }else{ ?>
                      <td style="background: #48C9A9">O</td>
                      <?php } }  ?>
              </tr>
              <?php }  ?>

      【讨论】:

        【解决方案4】:
        foreach($workdays as $w){
           foreach($tanggale as $t){
               if($w == $t){
                  $color = "#FFF000";
                  $text = "M";
               } else {
                  $color = "#48C9A9";
                  $text = "O";
               }
           }
               ?>
               <td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
        
        
        <?php }?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-06
          • 2021-12-20
          • 1970-01-01
          相关资源
          最近更新 更多