简体   繁体   中英

in_array() doesn't return all results

A database table named users has a column 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)

My php script should list the users who have a specified time-mark.

Query: SELECT * FROM ('users') (query result is returned to $users )

Loop:

  1. iterate through users
  2. create array $timemarks_arr for each user by splitting timemarks string using 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 . Does anyone have any idea why?

It won't match values like 17:00:00 , because there's a leading space.

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

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