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})
最后修改:2020 年 12 月 10 日 08 : 15 PM
对您有帮助的话,请赏包辣条吧 ^~^