function to_slash($array)
{
// 先转成json字符串,进行正则替换,再转换为数组
$tmp = json_encode($array);
$tmp = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $tmp));
$tmp = json_decode($tmp, true);
return $tmp;
}
原理:
// 驼峰转下划线
// 先添加分隔符,再转成小写
// userId => user_id
$str = preg_replace('/((?<=[a-z])(?=[A-Z]))/','_',$str);
$str = strtolower($str);
// 下划线转驼峰
// 同样先添加分隔符,再转成大写
// user_id => userId
$array = explode('_',$str);
array_walk($array,create_function('&$v','$v=ucwords($v);'));
$str = implode('',$array);
// 首字母转小写
$str{0} = strtolower($str{0})
版权属于:
小破孩
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论