English 中文(简体)
对比阵列中的缺失数字。 PHP
原标题:Comparing missing numbers in an array. PHP

我看上了本表所花时间,然后将这些时间从这三个阵列中删除。 阵列为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...

}
?>
最佳回答

你们可以不像你目前的战略那样,只储存每一次阵列的数量:

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 = array_keys(array_filter($timelist,  lessThan3 ));

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

<>Edit,可变数量服务器。

// 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的注射攻击。 有些人如果得不到适当的保护,就可以删除整个数据库,或者更糟。 请阅读:

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签