简体   繁体   English

in_array()不返回所有结果

[英]in_array() doesn't return all results

A database table named users has a column timemarks . 名为users的数据库表具有列时间timemarks

The timemark fields look like this.. 11:00:00, 13:45:00, 17:00:00, 18:25:00 (time-marks vary for each user) 所述timemark字段看起来像这样.. 11:00:00, 13:45:00, 17:00:00, 18:25:00 (时间标记为每个用户而异)

My php script should list the users who have a specified time-mark. 我的php脚本应该列出具有指定时间标记的用户。

Query: SELECT * FROM ('users') (query result is returned to $users ) 查询: SELECT * FROM ('users') (查询结果返回给$users

Loop: 环:

  1. iterate through users 迭代用户
  2. create array $timemarks_arr for each user by splitting timemarks string using str_getcsv 通过使用str_getcsv拆分时间字符串,为每个用户创建数组$timemarks_arr str_getcsv
  3. echo users with specified time-mark value 回显具有指定时间标记值的用户
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;

For some reason it echos only 2 users but there are more with time-mark 17:00:00 . 出于某种原因,它只回复了2个用户,但时间标记为17:00:00 Does anyone have any idea why? 有谁知道为什么?

It won't match values like 17:00:00 , because there's a leading space. 它不会匹配像17:00:00这样的值,因为它有一个领先的空间。

Try this to strip away all whitespace from the beginning and end of strings: 试试这个从字符串的开头和结尾去除所有空格:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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