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 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错