处理重复数组


Dealing with duplicate arrays

我有一个我正在开发的扫描仪项目,其中一个类读取由扫描仪创建的txt文件并查询数据库以显示每个房间的扫描仪的"捆绑包"扫描仪。然而,现在我有一些房间复制的结果。查询的结果每天在数组中停止并进行操作。我想做的是显示唯一的捆绑,并删除重复留下最早的条目。下面是我的索引页。任何提示和建议都将非常感激。

         <?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(13);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// Initialise the scanner class
require_once "/var/www/Scanners/scanners.php";
$scanner = new CHScanners();
$_GET['date'] = date("Y-m-d", $scanner->GetPreviousMonday(strtotime($_GET['date'])));
$weeklyTotal = 0;
$content = "<h1>" . $scanner->GetRoomName($_GET['room']) . " Information</h1>
<form action='room.php' method='get'>
    Enter date: <input type='text' name='date' /><input type='submit' /><input type='hidden' name='room' value='" . $_GET['room'] ."' />
</form>
<table width='100%'>
    <tr>
        <td style='vertical-align: top;'>
            <table width='100%'>
                <tr>
                    <th colspan='2'>Monday</th>
                </tr>
                <tr>
                    <th>Bundle #</th>
                    <th>Qty</th>
                </tr>";
            $result = $scanner- 
              >ListRoomTotals($_GET['room'],$_GET['date']);
    $total = 0;
    foreach($result as $x) {
    $content .= "<tr>
        <td>" . $x[0] . "</td>
        <td>" . $x[1] . "</td>
    </tr>";
    $total += $x[1];
    }
    $weeklyTotal += $total;
           $content .= "<tr><td>Total Pairage:</td><td>".$total."                      
        </td></tr>
        <tr><td>Total Dozens:</td><td>".number_format($total/12,1)."</td></tr></table>
      </td> 
        <td style='vertical-align: top;'>
            <table width='100%'>
                <tr>
                    <th colspan='2'>Tuesday</th>
                </tr>
                <tr>
                    <th>Bundle #</th>
                    <th>Qty</th>
                </tr>";
            $date = date("Y-m-d",(strtotime($_GET['date']) + 86400));
            $result = $scanner->ListRoomTotals($_GET['room'], $date);
            $total = 0;
            foreach($result as $x) {
                $content .= "<tr>
                    <td>" . $x[0] . "</td>
                    <td>" . $x[1] . "</td>
                </tr>";
                $total += $x[1];
            }
            $weeklyTotal += $total;
            $content .= "<tr><td>Total Pairage:</td><td>" . $total . "
                        </td></tr>
                    <tr><td>Total Dozens:</td><td>" .  
                  number_format($total/12,1) . "</td></tr></table>
        </td>
        <td style='vertical-align: top;'>
            <table width='100%'>
                <tr>
                    <th colspan='2'>Wednesday</th>
                </tr>
                <tr>
                    <th>Bundle #</th>
                    <th>Qty</th>
                </tr>";
            $date = date("Y-m-d",(strtotime($_GET['date']) +  
                           (86400*2)));
            $result = $scanner->ListRoomTotals($_GET['room'], $date);
            $total = 0;
            foreach($result as $x) {
                $content .= "<tr>
                    <td>" . $x[0] . "</td>
                    <td>" . $x[1] . "</td>
                </tr>";
                $total += $x[1];
            }
            $weeklyTotal += $total;
            $content .= "<tr><td>Total Pairage:</td><td>" . $total . "
                          </td></tr>
                    <tr><td>Total Dozens:</td><td>" . 
                   number_format($total/12,1) . "</td></tr></table>
        </td>
        <td style='vertical-align: top;'>
            <table width='100%'>
                <tr>
                    <th colspan='2'>Thursday</th>
                </tr>
                <tr>
                    <th>Bundle #</th>
                    <th>Qty</th>
                </tr>";
            $date = date("Y-m-d",(strtotime($_GET['date']) + 
                      (86400*3)));
            $result = $scanner->ListRoomTotals($_GET['room'], $date);
            $total = 0;
            foreach($result as $x) {
                $content .= "<tr>
                    <td>" . $x[0] . "</td>
                    <td>" . $x[1] . "</td>
                </tr>";
                $total += $x[1];
            }
            $weeklyTotal += $total;
            $content .= "<tr><td>Total Pairage:</td><td>" . $total . " 
                        </td></tr>
                    <tr><td>Total Dozens:</td><td>" .  
                 number_format($total/12,1) . "</td></tr></table>
        </td>
        <td style='vertical-align: top;'>
            <table width='100%'>
                <tr>
                    <th colspan='2'>Friday</th>
                </tr>
                <tr>
                    <th>Bundle #</th>
                    <th>Qty</th>
                </tr>";
            $date = date("Y-m-d",(strtotime($_GET['date']) + 
                       (86400*4)));
            $result = $scanner->ListRoomTotals($_GET['room'], $date);
            $total = 0;
            foreach($result as $x) {
                if($x[0] != "" and isset($x[0])) {
                    $content .= "<tr>
                        <td>" . $x[0] . "</td>
                        <td>" . $x[1] . "</td>
                    </tr>";
                    $total += $x[1];
                }
            }
            $weeklyTotal += $total;
            $content .= "<tr><td>Total Pairage:</td><td>" . $total . "
             </td></tr>
                    <tr><td>Total Dozens:</td><td>" .  
             number_format($total/12,1) . "</td></tr></table>
        </td>
    </tr>
</table>";
$options .= "Weekly Pairs: " . $weeklyTotal . "<br>
            Weekly Dozens: " . $weeklyTotal/12;
$template->SetTag("options", $options);
$template->SetTag("content", $content);
echo $template->Display();
     ?>

在追加之前做一个in_array()测试。

在查询中实现约束可能比在PHP中实现约束更好。试着在你的约会中使用group by和min来获得最早进入的房间。以下是本网站的相关示例。

如果你想在PHP中做,你需要一些过滤器。设置一个临时数组来存储最终结果,并循环遍历所有结果。如果房间不在结果数组中,则添加它。如果是,比较日期,使用最早的日期