简体   繁体   中英

Comparing Array Dates in PHP

I have two lists of Dates in MySQL tables with the following outputs:

$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:

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 . 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). Does anyone have suggestions on how to compare $BetGameDate[$i] with all dates in $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].

Not sure why you are iterating through the list of results in GameDate inside the for loop of BetDate.

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. But I guess the array_intersect is easier to implement.

EDIT: just saw that BetDate isnt as long as BetGameDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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