dqrzot2791 2014-02-10 07:42
浏览 50
已采纳

消息:不能将标量值用作数组

In Codeigniter I am creating some helpers to check user in certain group/s by allowing passing group name as a string or an array for multiple groups. Now seems things are working fine if I don't pass userid for $id argument. When I set the $id to some userid like 2 it is giving an error below

Message: Cannot use a scalar value as an array

Here is my all helper functions and than usage.

Getting user's groups

if ( !function_exists('get_user_group') )
{
    function get_user_group($id = FALSE)
    {
    $CI =& get_instance();

    // getting user groups by id or logged in
    $object = $CI->ion_auth->get_users_groups($id)->result();

    foreach($object as $group):
        $id[] = $group->id;
        $groups[] = $group->name;
    endforeach;
    $groups = array_combine($id, $groups);

    return $groups;

    }
}

Check if User in the group/s

if ( !function_exists('in_group') )
{
    function in_group($groups, $id = FALSE)
    {
    if(is_array($groups)){

        foreach($groups as $group){
        //print_r($group);
        if(in_array($group, get_user_group($id))){
            return TRUE;
        }
        }

    } elseif(is_string($group)) {
        if(in_array($group, get_user_group($id))){
        return TRUE;
        }
    }
    }
}

Usage

if(in_group(array('hr'), 2)){
    echo '<h1>IS IN THIS GROUP</h1>';
} else {
    echo '<h1>IS NOT IN THIS GROUP</h1>';
}
  • 写回答

1条回答 默认 最新

  • douzhang1852 2014-02-10 09:07
    关注

    As per @kumar_v comment I have modified `get_user_group() helper as below and solved the issue.

    if ( !function_exists('get_user_group') )
    {
        function get_user_group($uid = FALSE)
        {
        $CI =& get_instance();
    
        // getting user groups by id or logged in
        $object = $CI->ion_auth->get_users_groups($uid)->result();
    
        foreach($object as $group):
            $id[] = $group->id;
            $groups[] = $group->name;
        endforeach;
        $groups = array_combine($id, $groups);
    
        return $groups;
    
        }
    }
    

    Hope this may helps to others

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵