在数组中与特定条件值进行比较

时间:2014-07-18 10:12:04

标签: php arrays

我在loping之后得到数组

while($num_rows1 = mysql_fetch_array($data_query_details1))

现在我想检查我的特定user_id(假设这里是user_id = 3)是否在数组中保持2/3(我的具体值)的时间与print ok相同print need more

Array
(
    [0] => 3
    [user_id] => 3
    [1] => 2014-07-18
    [date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
    [0] => 3
    [user_id] => 3
    [1] => 2014-07-18
    [date(FROM_UNIXTIME(date))] => 2014-07-18
)
Array
(
    [0] => 4
    [user_id] => 4
    [1] => 2014-07-18
    [date(FROM_UNIXTIME(date))] => 2014-07-18
)

任何逻辑?

2 个答案:

答案 0 :(得分:0)

$dates = array();
$stay = false;
$userid_to_check = 3;
while($row = mysql_fetch_array($data_query_details1)){
    if($row['user_id']==$userid_to_check){
        if(in_array($row[1], $dates)){
            $stay = true;
            break;
        }
        $dates[] = $row[1];
    }
}
if($stay)echo 'ok';
else echo 'need more';

最好使用更直接的数据库选择,如上面的答案。 例如,codebird提到了什么:

$query = "select count(*) from your_table GROUP BY user_id HAVING date(FROM_UNIXTIME(date))='2014-07-18'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count>=2)echo 'ok';
else echo 'need more';

答案 1 :(得分:0)

这可以解决您的问题:

SELECT user_id, COUNT(*) FROM your_table 
WHERE DATE(FROM_UNIXTIME(date))='2014-07-18' 
GROUP BY user_id