简体   繁体   English

比较PHP中的数组日期

[英]Comparing Array Dates in PHP

I have two lists of Dates in MySQL tables with the following outputs: 我在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" }

Here is some of my PHP code: 这是我的一些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";
    }
}

However, I would like to compare the dates in the following manner: I want to loop through $BetGameDate , seeing if ANY dates in $BetDate match the ith selection of $BetGameDate . 不过,我想日期以下列方式比较:我想遍历$BetGameDate ,看是否在任何日期$BetDate比赛的第i个选择$BetGameDate The code that I have does not echo the result, and what's really confusing me is when I change $BetDate to $BetDate[$i] in the if statement, it echo's Hello2 twice, when only one selection should be equal (the 2nd value, 1st position in the arrays). 我拥有的代码没有回显结果,真正令我困惑的是,当我在if语句$BetDate $BetDate[$i]更改$BetDate $BetDate[$i]时,当只有一个选择应该相等时,它回声两次Hello2(第二个值,在数组中的第一位置)。 Does anyone have suggestions on how to compare $BetGameDate[$i] with all dates in $BetDate[] ? 有没有人对如何将$BetGameDate[$i]$BetDate[]所有日期进行比较提出建议?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

试试这个功能: array_intersect

It's quite simple: 很简单:

<?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';
    }
}
?>

Just change your $BetDate to $BetDate[$i]. 只需将$ BetDate更改为$ BetDate [$ i]。

Not sure why you are iterating through the list of results in GameDate inside the for loop of BetDate. 不知道为什么要遍历BetDate的for循环内GameDate中的结果列表。

I'm thinking you might want to do: 我在想您可能要这样做:

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";
        }
    }
}

this should compare each of BetDate with each of BetGameDate. 这应该将每个BetDate与每个BetGameDate进行比较。 But I guess the array_intersect is easier to implement. 但是我猜想array_intersect更容易实现。

EDIT: just saw that BetDate isnt as long as BetGameDate 编辑:只是看到BetDate与BetGameDate一样长

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

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