繁体   English   中英

比较PHP中的数组日期

[英]Comparing Array Dates in PHP

我在MySQL表中有两个日期列表,具有以下输出:

$BetDate - array(25) { [0]=> int(1355468400) [1]=> int(1355468400) [2]=> int(1355468400) [3]=> int(1355468400) [4]=> int(1355468400) [5]=> int(1355468400) [6]=> int(1355295600) [7]=> int(1355295600) [8]=> int(1355295600) [9]=> int(1355295600) [10]=> int(1355468400) [11]=> int(1355468400) [12]=> int(1355209200) [13]=> int(1355209200) [14]=> int(1355209200) [15]=> int(1355295600) [16]=> int(1355209200) [17]=> int(1355209200) [18]=> int(1355468400) [19]=> int(1355468400) [20]=> int(1355554800) [21]=> int(1355554800) [22]=> int(1355554800) [23]=> int(1355554800) [24]=> int(1355554800) } 
array(25) { [0]=> string(10) "2012-12-14" [1]=> string(10) "2012-12-14" [2]=> string(10) "2012-12-14" [3]=> string(10) "2012-12-14" [4]=> string(10) "2012-12-14" [5]=> string(10) "2012-12-14" [6]=> string(10) "2012-12-12" [7]=> string(10) "2012-12-12" [8]=> string(10) "2012-12-12" [9]=> string(10) "2012-12-12" [10]=> string(10) "2012-12-14" [11]=> string(10) "2012-12-14" [12]=> string(10) "2012-12-11" [13]=> string(10) "2012-12-11" [14]=> string(10) "2012-12-11" [15]=> string(10) "2012-12-12" [16]=> string(10) "2012-12-11" [17]=> string(10) "2012-12-11" [18]=> string(10) "2012-12-14" [19]=> string(10) "2012-12-14" [20]=> string(10) "2012-12-15" [21]=> string(10) "2012-12-15" [22]=> string(10) "2012-12-15" [23]=> string(10) "2012-12-15" [24]=> string(10) "2012-12-15" }    

$BetGameDate - array(4) { [0]=> int(1355554800) [1]=> int(1355468400) [2]=> int(1355900400) [3]=> int(1355554800) }
array(4) { [0]=> string(10) "2012-12-15" [1]=> string(10) "2012-12-14" [2]=> string(10) "2012-12-19" [3]=> string(10) "2012-12-15" }

这是我的一些PHP代码:

foreach($checkit as $row1) {
    $BetDate[] = strtotime($row1['BetDate']);
    }
    foreach($result as $row) {
    $BetGameDate[] = strtotime($row['GameDate']);
    }
    for($i=0; $i<=$count; $i++) { 
if($BetDate==$BetGameDate[$i]) {
    echo "Hello2";
    }
}

不过,我想日期以下列方式比较:我想遍历$BetGameDate ,看是否在任何日期$BetDate比赛的第i个选择$BetGameDate 我拥有的代码没有回显结果,真正令我困惑的是,当我在if语句$BetDate $BetDate[$i]更改$BetDate $BetDate[$i]时,当只有一个选择应该相等时,它回声两次Hello2(第二个值,在数组中的第一位置)。 有没有人对如何将$BetGameDate[$i]$BetDate[]所有日期进行比较提出建议?

任何帮助是极大的赞赏!

试试这个功能: array_intersect

很简单:

<?php
$array1 = array('1', '2', '3');
$array2 = array('0', '2', '0');
$length = count($array1);

for ($i = 0; $i < $length; $i++) {
    $key = array_search($array1[$i], $array2);

    if ($key != null && $key > 0) {
        echo 'HELLO 2';
    }
}
?>

只需将$ BetDate更改为$ BetDate [$ i]。

不知道为什么要遍历BetDate的for循环内GameDate中的结果列表。

我在想您可能要这样做:

foreach($checkit as $row1) {
    $BetDate[] = strtotime($row1['BetDate']);
}
foreach($result as $row) {
    $BetGameDate[] = strtotime($row['GameDate']);
}
$matchArray = array_intersect($BetDate, $BetGameDate);
for($BetGameDate as $b) { 
    for($BetDate as $bet){
        if($b == $bet) {
            echo "Hello2";
        }
    }
}

这应该将每个BetDate与每个BetGameDate进行比较。 但是我猜想array_intersect更容易实现。

编辑:只是看到BetDate与BetGameDate一样长

暂无
暂无

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

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