madcrazydrumma madcrazydrumma - 1 year ago 99
JSON Question

Checking if a json object's name is equal to something

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";

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download