push and in
I am having a bit of difficulty with this. I want to allow a user to check if a username is available through an AJAX request. The AJAX request calls my php and PHP returns true if the username is not available or false if available.
I wanted to merge the username into the array (if found) and then use in_array to locate a match. It isn't working this way however.
$res = // database returns any username that matches - (not an array)
$banned = // database returns an assoc array of banned names
array_push($banned, strtolower($res['user']));
if(!in_array(strtolower($requested), $banned)){
echo 'available';
} else {
echo 'not available';
}
Here is a sample array from the banned variable:
Array
(
[0] => bad1
[1] => bad2
[3] =>
)
The 3rd key is null because it wasn't found in the $res variable.
Is there a better way to do this? I also need to convert the values in the array to lowercase as well.
为了便于阅读,我认为这看起来会更好
if (isset($res['user'])) { // is this key set for this array?
$banned[] = strtolower($res['user']); // append the strtolower`d version
}
链接地址: http://www.djcxy.com/p/59416.html
上一篇: 如果第二个表的结果存在,则向PHP中的数组添加数组
下一篇: 推入