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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题