簡體   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