moodle auth using contextid in array
Can anyone shed any light as to why this is not working:
$USER->id
is the logged in user.
$contextroles = get_records_sql("SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid = 23 AND roleid = 3");
if (开发者_StackOverflowin_array($USER->id, $contextroles)) {
echo'your in<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
else{
echo'Access denied<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
This is the output:
Access denied
5410
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 ) )
Any help would be greatly appreciated.
$contextroles is an array of objects and the needle you are searching is within the object. in_array cannot handle objects and hence it fails. You can use the following code snippet to get the desired results:
http://www.php.net/manual/en/function.in-array.php#105937
精彩评论