简体   繁体   English

比较mysql和php中的两个数组

[英]Compare two arrays in mysql and php

i have this small loop of time in day. 我一天中的时间很少。

$datum = $date_final;
$iTimestamp = mktime(8,0,0,1,1,2014);
for ($i = 1; $i < $den; $i++) {
    $vypis = $datum.' '.date('H:i:s', $iTimestamp) . "\n<br />";
    $iTimestamp += $sekundy;
    echo $vypis;
}

It will echo something like this: 它将回显如下内容:

2014-06-02 10:00:00 
2014-06-02 10:30:00 
2014-06-02 11:00:00 
2014-06-02 11:30:00 
2014-06-02 12:00:00 
2014-06-02 12:30:00 
2014-06-02 13:00:00 
2014-06-02 13:30:00 
2014-06-02 14:00:00 
2014-06-02 14:30:00 
2014-06-02 15:00:00 
2014-06-02 15:30:00 
2014-06-02 16:00:00 
2014-06-02 16:30:00 
2014-06-02 17:00:00 

i have second script - mysql query loop 我有第二个脚本-mysql查询循环

$data = mysql_query ("select * from evenement");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
echo $zaznam["start"];
}

it give me similar results : 它给我类似的结果:

2014-05-25 19:30:00
2014-05-28 13:30:00
2014-04-21 09:30:00
2014-05-20 11:00:00
2014-05-28 13:00:00
2014-04-27 00:00:00
2014-05-21 12:00:00
2014-05-22 11:30:00
2014-05-20 13:00:00
2014-05-22 11:30:00
2014-05-28 15:30:00
2014-05-28 13:00:00

Im trying to write a script which compare every single echo $vypis with all results in mysql query. 我试图写一个脚本来比较每个回显$ vypis与mysql查询中的所有结果。 After that i can easily put condition here - if it is not in mysql - echo "Free" and if it is in mysql echo "....". 之后,我可以轻松地将条件放在此处-如果它不在mysql中,则回显“ Free”,如果它在mysql中,则回显“ ....”。 I think i have to use foreach maybe. 我想我可能必须使用foreach。 I need compare one result from php loop with all results from DB, apply conditions and next one result, compare to all ..... 我需要将php循环的一个结果与DB的所有结果进行比较,应用条件和下一个结果,与所有结果进行比较.....

Have you any ideas guys ? 你们有什么想法吗? I need small help here. 我在这里需要一点帮助。

to compare php array value with all sql values do like below (i hope this is what you are asking about) 将php数组值与所有sql值进行比较,如下所示(我希望这是您要问的问题)

for ($i = 1; $i < $den; $i++) {
    $vypis = $datum.' '.date('H:i:s', $iTimestamp) . "\n<br />";
    $iTimestamp += $sekundy;
    $arr1[]=$vypis;//store php values in first array
}

//and in php

while($zaznam = mysql_fetch_array ($data))
{
 $arr2[] = $zaznam["start"];//store query values in second array
}

//and now use forloops

foreach($arr1 as $k=>$val)
{
  if(in_array($val,$arr2))
  {
   echo '...';
  }
 else
 {
  echo 'Free';
 }
}

Store the results in an array for php 将结果存储在php数组中

$phpDateArray=array();
$datum = $date_final;
$iTimestamp = mktime(8,0,0,1,1,2014);
for ($i = 1; $i < $den; $i++) {
    $vypis = $datum.' '.date('H:i:s', $iTimestamp) . "\n<br />";
    $iTimestamp += $sekundy;
    $phpDateArray[]=$vypis;
}

Same for the query: 与查询相同:

$mysqlDataArray=array();
$data = mysql_query ("select * from evenement");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
    $mysqlDataArray[]=$zaznam["start"];
}

And search with array_search() 并使用array_search()搜索

foreach($phpDataArray as $key=>$time){
    if(array_search($time, $mysqlDataArray)===FALSE){
        echo $time. " free";
    } else {
        echo $time. " ...";
    }
}

This could be optimized, but i followed your style. 可以优化,但是我遵循您的风格。

An other options with array_merge would be like this: array_merge的其他选项如下:

$phpDateArray=array();
$datum = $date_final;
$iTimestamp = mktime(8,0,0,1,1,2014);
for ($i = 1; $i < $den; $i++) {
    $vypis = $datum.' '.date('H:i:s', $iTimestamp) . "\n<br />";
    $iTimestamp += $sekundy;
    $phpDateArray[$vypis]="free";
}

$mysqlDataArray=array();
$data = mysql_query ("select * from evenement");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
    $mysqlDataArray[$zaznam["start"]]="start";
}

$resultArray = array_merge($phpDateArray, $mysqlDataArray);
foreach($resultArray as $time=>$value){
    echo $value;
}

If you only want to find the differences you can use array_diff . 如果只想查找差异,则可以使用array_diff But if you want to check each one of the element you can do as @Feroz Akbar says 但是,如果您想检查每个元素,可以按照@Feroz Akbar所说的做

Using a temp table could be done like this:- 使用临时表可以像这样完成:

<?php

$datum = $date_final;
$iTimestamp = mktime(8,0,0,1,1,2014);
for ($i = 1; $i < $den; $i++) 
{
    $vypis = $datum.' '.date('H:i:s', $iTimestamp) . "\n<br />";
    $aDate[] = $vypis;
    $iTimestamp += $sekundy;
    echo $vypis;
}

$sql = "CREATE TEMPORARY TABLE tmp_date (aDate DATETIME, INDEX aDate(aDate))";
$data = mysql_query ($sql);
$sql = "INSERT INTO tmpdate(aDate) VALUES ('".implode("','", $aDate)."')";
$data = mysql_query ($sql);

$sql = "SELECT a.start
        FROM evenement a
        INNER JOIN tmp_date b
        ON a.start = b.aDate";
$data = mysql_query($sql);
$zaznam = mysql_fetch_array ($data);
echo "List if start dates that are in the generated list of dates\r\n";
while($zaznam = mysql_fetch_array ($data))
{
    echo $zaznam["start"]."\r\n";
}

$sql = "SELECT a.start
        FROM evenement a
        LEFT OUTER JOIN tmp_date b
        ON a.start = b.aDate
        WHERE b.aDate IS NULL";
$data = mysql_query($sql);
$zaznam = mysql_fetch_array ($data);
echo "List if start dates that are not in the generated list of dates\r\n";
while($zaznam = mysql_fetch_array ($data))
{
    echo $zaznam["start"]."\r\n";
}

$sql = "SELECT a.aDate
        FROM tmp_date b
        INNER JOIN evenement a
        ON a.start = b.aDate
        WHERE a.start IS NULL";
$data = mysql_query($sql);
$zaznam = mysql_fetch_array ($data);
echo "List if generated dates that are not in the list of start dates\r\n";
while($zaznam = mysql_fetch_array ($data))
{
    echo $zaznam["start"]."\r\n";
}

?>

The temp table is only there for the length of time the database connection exists (and only for that database connection). 临时表仅在数据库连接存在的时间内存在(并且仅在该数据库连接存在)。 Easy to create it and insert the rows to it as you can see. 轻松创建它并在其中插入行。

The rest of the script is putting out the dates, either as ones that in both lists, in one list or in the other list. 脚本的其余部分以两个列表,一个列表或另一个列表中的日期为日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM