您的位置 首页 php

php 将数组中以下划线连接的键转为驼峰

php中在做接口的时候,有时候接口定义规范约束返回字段为驼峰,

而PHP中直接取数据表中字段的时候,一般都是以下划线分割的,

所以要对其进行转换,方法如下:

//多维数组中,将数组中键为下划线的转为驼峰

function convertArrKey($list) {
$new = array();
if (is_object($list)) {
$list = json_decode(json_encode($list), true);
}
if (is_array($list)) {
$list = convertKeyUp($list);
 foreach  ($list as $k => $v) {
$new[$k] = convertArrKey($v);
}
return $new;
}
return $list;
}
 

//去掉下划线,’_’后面的首字母大写

function convertKeyUp($array) {
if (!is_array($array) && !is_object($array)) {
return $array;
}
$new = array();
foreach ($array as $k => $v) {
$temp =  explode ('_', $k);
$i = 0;
foreach ($temp as $kk => $vv) {
if ($i > 0) {
$temp[$kk] = ucfirst($vv);
}
$i++;
}
$temp = implode('', $temp);
$new[$temp] = $v;
}
unset($array);
return $new;
}
 

文章来源:智云一二三科技

文章标题:php 将数组中以下划线连接的键转为驼峰

文章地址:https://www.zhihuclub.com/78627.shtml

关于作者: 智云科技

热门文章

网站地图