比较数组中缺少的数字。 PHP

时间:2011-08-15 06:09:32

标签: php mysql arrays time compare

我得到了这个脚本,它在这个表中查找了一些时间,然后从这三个数组中删除了那些时间。阵列是1-24次。

此脚本的最终目标是比较这些数组中所有丢失的时间,并使用一个只有可用时间的大数组。

需要检查是否连续三次丢失时间。如果是,则该时间不会显示在最终数组中。

例如:

<?php

include 'db-connect.php';
if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) {

$month = $_GET['month'];
$day = $_GET['day'];
$year = $_GET['year'];

//string together date
$date = $month."/".$day."/".$year;

//define the queries
$sql1 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '1'");
$sql2 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '2'");
$sql3 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '3'");

//define time lists for each server
$timelist1 = range(1, 24);
$timelist2 = range(1, 24);
$timelist3 = range(1, 24);

//unset the arrays with the taken times for server 1
while($query1 = mysql_fetch_array($sql1)) {

    unset($timelist1[$query1['start_time'] - 1]);

}
//unset the arrays with the taken times for server 2
while($query2 = mysql_fetch_array($sql2)) {

    unset($timelist2[$query2['start_time'] - 1]);

}
//unset the arrays with the taken times for server 3
while($query3 = mysql_fetch_array($sql3)) {

    unset($timelist3[$query3['start_time'] - 1]);

}

//now see which times are missing three times in a row and make one final array of available times.

//code goes here...

}
?>

1 个答案:

答案 0 :(得分:0)

而不是您当前的策略,您可以在每次找到时存储数组的数量,例如:

include 'db-connect.php';
if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) {

$month = $_GET['month'];
$day = $_GET['day'];
$year = $_GET['year'];

//string together date
$date = $month."/".$day."/".$year;

//define time list
$timelist = array_fill(1, 24, 0);

while($query1 = mysql_fetch_array($sql1)) {

    $timelist[$query1['start_time']]++;

}

while($query2 = mysql_fetch_array($sql2)) {

    $timelist[$query2['start_time']]++;

}

while($query3 = mysql_fetch_array($sql3)) {

    $timelist[$query3['start_time']]++;

}

$timelist = array_keys(array_filter($timelist, 'equals3'));

function equals3($x){
    return $x == 3;
}

现在$ timelist是在所有三个查询中找到的时间数组。如果您想要从至少一个查询中缺少一些次数,请使用此次而不是最后几行:

$timelist = array_keys(array_filter($timelist, 'lessThan3'));

function lessThan3($x){
    return $x < 3;
}

编辑以获取可变数量的服务器。

// Here you can list the servers you want to include in the query
// In an array form so it can be filled easily
$servers = array(1,2,4,5);
$num_servers = count($servers);

// Convert servers into queryable format
$servers = '(\''.implode('\',\'', $servers).'\')';
$query = 'SELECT `start_time`, COUNT(*) as `count` FROM `classes`
           WHERE `date` = \''.$date.'\' AND `server` IN '.$servers.' 
           GROUP BY `start_time`';
$result = mysql_query($query);

$timelist = array_fill(1, 24, 1);
while($row = mysql_fetch_row($result))
    if($row[1] == $num_servers) // `start_time` is missing from at least one server
        unset($timelist[$row[0]]);
$timelist = array_keys($timelist);

// Now $timelist is an array of times that are available on at least one server from the query

请注意,由于您的日期变量来自$_GET,因此您应该非常小心MySQL注入攻击。有人可能会删除您的整个数据库,或者更糟糕的是,如果您没有适当的保护措施。请阅读:http://www.learnphponline.com/security/sql-injection-prevention-mysql-php