【问题标题】:Comparing missing numbers in an array. PHP比较数组中缺失的数字。 PHP
【发布时间】:2011-08-15 06:09:32
【问题描述】:

我得到了这个脚本,它在这个表中查找所用的时间,然后从这三个数组中删除这些时间。数组是 1-24 倍。

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

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

例如:

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

}
?>

【问题讨论】:

  • 您不需要为每个服务器值运行单独的查询。您也不需要(或不想)在查询中为数字加上引号。

标签: php mysql arrays time compare


【解决方案1】:

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

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

【讨论】:

  • 所以这个脚本从 $timelist 数组中删除了所有占用的时间?
  • @David 我不确定什么是“花费时间”。上面的脚本创建了一个数组,其中包含 1 到 24 之间的数字,这些数字在所有三个查询 start_time 中都可以找到。
  • 当您调用 mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '1'") 时,它会提取该日期的不可用时间。
  • @David 在这种情况下,$timelist 是一个在所有三台服务器上都不可用的时间数组。如果你想要相反的(所以数组包含至少一个服务器上可用的时间,请使用我刚刚编辑的代码。
  • 解析错误:语法错误,意外的 ')',期望函数 equals3(x) 上的 '&' 或 T_VARIABLE{
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多