时间:2019-10-14 11:27:42
/**
* 字段过滤及下划线与驼峰转化 $this->filter($value,'*');
* @param array $oneData
* @param string $filed 默认* *所有
* @param int $type 默认1 1(下划线=>驼峰) 2(驼峰=>下划线)
*/
protected function filter(array &$oneData,$filed = "*", $type = 1){
empty($filed) && $filed = "*";
if(is_array($filed)){
$filed = implode(',',$filed);
}
$filed != "*" && $filed = ','.$filed;
$arrReturn = [];
array_walk($oneData, function ($item,$index) use ($type,$filed,&$arrReturn){
//过滤
if($filed == "*" || stripos($filed,','.$index) !== false){
$_index = '';
if(1 == $type){
/**
* 下划线=>驼峰
* 思路:
* step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
* step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
*/
$_index = '_'. str_replace('_', " ", strtolower($index));
$_index = ltrim(str_replace(" ", "", ucwords($_index)), '_' );
}else{
/**
* 驼峰=>下划线
* 思路:
* 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
*/
$_index = strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $index));
}
$arrReturn[$_index] = $item;
}
});
$oneData = $arrReturn;
unset($arrReturn);
}
PHP函数:字段过滤与命名风格转换,
该篇文章介绍了如何在PHP中使用一个保护方法`filter()`,对数组进行字段过滤,并处理下划线与驼峰命名的转换,支持两种转换类型:下划线转驼峰和驼峰转下划线。
2374

被折叠的 条评论
为什么被折叠?



