| 123456789101112131415161718192021222324252627282930 |
- <?php
- namespace app\utils;
- class ArrayUtil
- {
- /**
- * 将数组内的 keys下划线转驼峰
- *
- * @param array $arr
- * @param string $separator
- *
- * @return array
- */
- public static function underline2CamelArr(array $arr, string $separator = '_'): array
- {
- $result = [];
- if (!empty($arr)) {
- foreach ($arr as $words => $value) {
- if (strpos($words, $separator) !== false) {
- $words = $separator . str_replace($separator, " ", strtolower($words));
- $result[ltrim(str_replace(" ", "", ucwords($words)), $separator)] = $value;
- } else {
- $result[$words] = $value;
- }
- }
- }
- return $result;
- }
- }
|