in_array()不返回所有结果

时间:2012-06-04 02:54:38

标签: php mysql arrays loops foreach

名为users的数据库表有一列timemarks

timemark字段看起来像这样.. 11:00:00, 13:45:00, 17:00:00, 18:25:00(时间标记因用户而异)

我的php脚本应该列出具有指定时间标记的用户。

查询:SELECT * FROM ('users')(查询结果返回$users

循环:

  
      
  1. 遍历用户
  2.   
  3. 通过使用$timemarks_arr
  4. 拆分时间字符串,为每个用户创建数组str_getcsv   
  5. 使用指定的时间标记值回显用户
  6.   
foreach ($users as $user): 

   $timemarks_arr = str_getcsv($user->timemarks); //split time-marks by comma and create array

    if (in_array("17:00:00", $timemarks_arr)) //users with specified time-mark
    {                               
        echo $entry->username . "<br />";
    }

endforeach;

出于某种原因,它只回复了2个用户,但还有更多时间标记为17:00:00。有谁知道为什么?

1 个答案:

答案 0 :(得分:4)

它与17:00:00之类的值不匹配,因为它有一个前导空格。

尝试从字符串的开头和结尾去除所有空格:

$timemarks_arr = array_map( "trim", str_getcsv($user->timemarks)); //split time-marks by comma and create array
相关问题