处理重复的数组

时间:2011-11-18 15:04:52

标签: php 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();
     ?>

2 个答案:

答案 0 :(得分:0)

在追加之前进行in_array()测试。

答案 1 :(得分:0)

在查询中实现约束可能比在PHP中实现约束更好。尝试使用group by并在你的约会时使用min来获得最早入场的房间。 Here is a relevant example on this site.

如果您希望在PHP中执行此操作,则必须使用某些过滤器。设置临时数组以存储最终结果,并循环显示所有结果。如果房间不在结果数组中,请添加它。如果是,请比较日期并使用最早的日期。