php foreach将2个数组合并为1行(工作日和数据)

I have a problem with my current "Attendance Project", so I have 2 arrays.

  • 1st array is to show a "workdays" the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
  • 2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)

here is my current code :

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

It will produce : enter image description here

I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range. Any help will appreciate..

Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:

1st - run foreach ($workdays as $w) and make header 2nd - run foreach ($workdays as $w) and make table-body like:

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

Simply what u can do is combine the 2 arrays in to one and then iterate the combine array as per your requirment. Check below code for combining the array

<?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';
        }
    }
?>

This code will create combine array with key as day and value is present or Absent.

Now you can iterate as per your requirement below is the iteration code.

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

I hope this answers solves your question.

Do it in this way

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

</div>
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 }?>