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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了