您的位置 首页 php

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

上一篇中,将数组中以下划线连接的键转为驼峰,本篇记录将数组中键为驼峰的转为下划线连接

方法如下:

//数组中键为驼峰的转下划线

function convertHumpToLine(array $data) {
$result = [];
 foreach  ($data as $key => $item) {
if (is_array($item) || is_object($item)) {
$result[humpToLine($key)] = convertHumpToLine((array) $item);
} else {
$result[humpToLine($key)] =  trim ($item);
}
}
return $result;
}
 

//驼峰 字符串 转为下划线连接

 function  humpToLine($str) {
$str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
return '_' . strtolower($matches[0]);
}, $str);
return $str;
}
 

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

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

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

关于作者: 智云科技

热门文章

网站地图