如何比较两个数组并在列

时间:2016-03-24 04:11:15

标签: php html arrays html-table

我有两个阵列。

数组1 - 所有日期列表
数组2 - 一个人在场的所有日期列表

我想要显示一个表格,其中包含第一行中的所有日期以及第二行中某个人出现的每一天,相应的列应该表示存在。

我尝试了嵌套循环,但它只显示了每行的各种行和一个结果,即每行只有一个匹配。

我想完成这样的事情

<table width="100%" border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td width="6%">160111</td>
    <td width="6%">160113</td>
    <td width="6%">160120</td>
    <td width="6%">160127</td>
    <td width="6%">160201</td>
    <td width="6%">160203</td>
    <td width="6%">160208</td>
    <td width="6%">160210</td>
    <td width="6%">160217</td>
    <td width="6%">160224</td>
    <td width="6%">160229</td>
    <td width="6%">160302</td>
    <td width="6%">160307</td>
    <td width="6%">160309</td>
    <td width="6%">160321</td>
    <td width="5%">160323</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>Present</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>Present</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>Present</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

连连呢?如何运行循环或如何实现?

3 个答案:

答案 0 :(得分:2)

使用in_array()来测试某个项目是否在数组2中。

foreach ($array1 as $day) {
    echo "<td>";
    echo in_array($day, $array2) ? "Present" : "&nbsp;";
    echo "</td>";
}

答案 1 :(得分:0)

如果Array与我在答案中给出的相同,你可以试试这个

PHP代码:

<?php
    $dates =array(
        '160111','160113','160120','160127','160201','160203',
        '160208','160210','160217','160224','160229','160302',
        '160307','160309','160321','160323'  
    );
    $person_present =array(
        '','','','160127','','',
        '','160210','','','','160302',
        '','','',''  
    );

   foreach ($dates as $pkey => $day) 
   {
        if ( in_array($day, $person_present) ) {
            ?>
            <td>Present</td>
           <?php
        } else {
            ?>
            <td>&nbsp;</td>
           <?php
        }        
   }
?>

完整代码:

    <table width="100%" border="1" cellspacing="1" cellpadding="1">
      <tr>
        <td width="6%">160111</td>
        <td width="6%">160113</td>
        <td width="6%">160120</td>
        <td width="6%">160127</td>
        <td width="6%">160201</td>
        <td width="6%">160203</td>
        <td width="6%">160208</td>
        <td width="6%">160210</td>
        <td width="6%">160217</td>
        <td width="6%">160224</td>
        <td width="6%">160229</td>
        <td width="6%">160302</td>
        <td width="6%">160307</td>
        <td width="6%">160309</td>
        <td width="6%">160321</td>
        <td width="5%">160323</td>
      </tr>
      <tr>
 <?php
    $dates =array(
        '160111','160113','160120','160127','160201','160203',
        '160208','160210','160217','160224','160229','160302',
        '160307','160309','160321','160323'  
    );
    $person_present =array(
        '','','','160127','','',
        '','160210','','','','160302',
        '','','',''  
    );

   foreach ($dates as $pkey => $day) 
   {
        if ( in_array($day, $person_present) ) {
            ?>
            <td>Present</td>
           <?php
        } else {
            ?>
            <td>&nbsp;</td>
           <?php
        }        
   }
?>   
      </tr>
    </table>

答案 2 :(得分:0)

  1. 遍历Array1(foreach循环)
  2. 针对Array2( in_array()
  3. 检查每个值

    例如: -

    echo "<br/><table border='1' style='width:100%'><tr><td>";
    foreach($arr1 as $a1) {
    echo "<table border='1' style='display:inline;border:0px solid'>";
    echo "<tr><td>$a1</td></tr>";
        if(in_array($a1,$arr2)) {
            echo "<tr><td>Present</td></tr>";
        } else {
            echo "<tr><td> </td></tr>";
        }
        echo "</table>";
    }
    echo "</td></tr></table>";