am using following function to state friend function between two user but i get following error
Warning: in_array() expects parameter 2 to be array, string given in C:\Users\sanoj\Documents\NetBeansProjects\PDO friends 2\userData.php on line 60
line 60 is this if (in_array($friend, $f)) {
and function is bellow
function isFriend($user, $friend) {
$isFriend = false;
$friends = getUsersFriends($user);
foreach ($friends as $f) {
if (in_array($friend, $f)) {
$isFriend = true;
}
}
return $isFriend;
}
Just like the comments above, if $friends
is indeed just a flat array, just use in_array
alone. No need for foreach
. What happens is, you're applying in_array
of each string which doesn't make sense. You apply in_array
to the whole array of strings:
function isFriend($user, $friend) {
return in_array($friend, getUsersFriends($user));
}
Take note that Sanoj != sanoj
. It might be better to change all the caps of the collection of friends to compare against the strtolower
ed $friend
:
Idea:
function isFriend($user, $friend) {
return in_array(strtolower($friend), array_map('strtolower', getUsersFriends($user)));
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加