title: php convert stdClass object to array
body: <p>Could anyone shed any light as to why I can get this working.<br>
  I want to query an array to see if the <code>USER-&gt;id</code> that is currently logged in is assigned a specific role:</p>
  
  <pre><code>$contextroles = get_records_sql("SELECT userid FROM {$CFG-&gt;prefix}role_assignments WHERE contextid = 23 AND roleid = 3");
  
  function object2array($object) {
      if (is_object($object)) {
          foreach ($object as $key =&gt; $value) {
              $array[$key] = $value;
          }
      }
      else {
          $array = $object;
      }
      return $array;
  }
  
  $alloweduser = object2array($contextroles);
  
  if (in_array($USER-&gt;id, $alloweduser)) {
      echo'Your in&lt;br /&gt;';
      echo $USER-&gt;id.'&lt;br /&gt;';
      print_r($alloweduser);
  }
  else{
      echo'&lt;br /&gt;You do not have permission to acces this database.&lt;br /&gt;';
      echo $USER-&gt;id.'&lt;br /&gt;';
      print_r($alloweduser);
      exit;
  }
  </code></pre>
  
  <p>Im currently getting this output:</p>
  
  <blockquote>
    <p>You do not have permission to acces this database.</p>
    
    <p>5410</p>
    
    <p>Array ( [7] => stdClass Object ( [userid] => 7 ) [9] => stdClass Object ( [userid] => 9 ) [27] => stdClass Object ( [userid] => 27 ) [98] => stdClass Object ( [userid] => 98 ) [203] => stdClass Object ( [userid] => 203 ) [252] => stdClass Object ( [userid] => 252 ) [5410] => stdClass Object ( [userid] => 5410 ) ) </p>
  </blockquote>
  
  <p>As you can see 5410 is in the array so should not get accessed denied.
  Thanks in advance for any help.</p>