Getting the role ID from the global $user I'm very new to Drupal, so please bear with me if this is a dumb question. According to the globals API, the global $user object returns roles as an array:



[roles] =&gt; Array(
  [rid] =&gt; name
)


If I reference $user->roles, it gives me the role names.

Short of looping through like this:

foreach($user-&gt;roles as $key =&gt; $value){
    error_log("key: " . $key . " ; value: " . $value);
}


Is there any way to return the role ID (rid) instead of the role name? I need to do conditional processing based on roles, but some of our roles are company-specific, and contain special characters. It would be much easier to work with the ID instead.
You can use array_key_exists() to do this:

$rid = 42;

if (array_key_exists($rid, $user-&gt;roles)) {
  // Do stuff
}


If you just want an array of RIDs, you can use array_keys():

$rids = array_keys($user-&gt;roles);