0

MySQLテーブルに日付のリストが2つあり、次の出力があります。

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

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!

4

4 に答える 4

1

この関数を試してください: array_intersect

于 2012-12-17T22:06:07.517 に答える
0

それは非常に簡単です:

<?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] に変更するだけです。

于 2012-12-17T22:08:21.510 に答える
0

BetDate の for ループ内で GameDate の結果のリストを反復処理している理由がわかりません。

私はあなたがしたいかもしれないと思っています:

foreach($checkit as $row1) {
    $BetDate[] = strtotime($row1['BetDate']);
}
foreach($result as $row) {
    $BetGameDate[] = strtotime($row['GameDate']);
}
$matchArray = array_intersect($BetDate, $BetGameDate);
于 2012-12-17T22:11:35.320 に答える
0
for($BetGameDate as $b) { 
    for($BetDate as $bet){
        if($b == $bet) {
            echo "Hello2";
        }
    }
}

これにより、各 BetDate と各 BetGameDate が比較されます。しかし、array_intersect の方が実装しやすいと思います。

編集: BetDate が BetGameDate ほど長くないことがわかりました

于 2012-12-17T22:13:25.663 に答える