带下划线数组转换为驼峰式命名数组

https://blog.csdn.net/trh0123/article/details/79046221

写接口代码的时候经常要统一驼峰式命名返回。但数据库又是以下划线命名的字段。以下代码直接将数组转为驼峰式命名的数组。不管是几维数组。分享给大家

 

    /*
     * 将下划线命名数组转换为驼峰式命名数组
     * @pram $arr 原数组
     * @pram $ucfirst 首字母大小写,false 小写,TRUE 大写
     */
    public static function camelCase($arr,$ucfirst = FALSE)
    {
        if (!is_array($arr))
        {   //如果非数组原样返回
            return $arr;
        }
        $temp = [];
        $keys = '';
        foreach ($arr as $key => $value)
        {
            $key1=self::convertUnderline($key,FALSE);
            $value1=self::camelCase($value);
            $temp[$key1]=$value1;
        }
        return $temp;
    }
 
 
    //将下划线命名转换为驼峰式命名
    public static function convertUnderline($str, $ucfirst = true)
    {
        $str = ucwords(str_replace('_', ' ', $str));
        $str = str_replace(' ', '', lcfirst($str));
        return $ucfirst ? ucfirst($str) : $str;
    }

 

posted on 2019-08-04 20:48  ziyi_ang  阅读(414)  评论(0编辑  收藏  举报

导航