So I have a basic json file:
{
"User1": {
"id": 1,
....
},
"User2": {
"id": 3
....
}
}
I want to get all the names of the users (i.e. "User1" or "User2") and check if a name entered by a user is equal to one of those names.
I've tried this but it doesn't work:
foreach($this->blackList as $user) {
if($user == $username)
return "TRUE";
}
return "FALSE";
}
I don't think you need to loop at all:
return array_key_exists($username, $this->blackList);
Or if you have json_decoded
to an object instead of an array:
return property_exists($this->blackList, $username);
I assume that $this->blackList is decoded your json string. If yes, then code should look like this:
foreach($this->blackList as $blackName => $user) {
if($blackName == $username)
return true;
}
}
return false;