I have a simple function that should just give me TRUE or FALSE if a value is find in an array.
function bypass($user, $bypassUsers){
$users = explode(",", $bypassUsers);
// trim($users);
if(in_array($user,$users)){
return true;
} else {
return false;
}
}
While to me everything looks of when I have more than 2 values in the array, the function returns FALSE as if in_array()
does not see from key [2].
Any idea?
If you want to apply trim to all elements, instead of:
$users = explode(",", $bypassUsers);
trim($users);
You should do this instead:
$users = array_map('trim', explode(',', $bypassUsers));
It applies trim()
to the result of explode()
. Afterwards, you can return the result in one statement:
return in_array($user, $users, true);
// third argument determines whether to use == or === for comparison
function bypass($user, $bypassUsers){
$users = explode(",", $bypassUsers);
foreach($users as $key=>$usr){
$users[$key] = trim($usr);
}
if(in_array(trim($user),$users)){
return true;
} else {
return false;
}
}
Trim is the problem because it works with string not with an 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.