| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?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;
- }
- /**
- * 根据 code 获取 name
- * @param $code
- * @param array $data
- * @return mixed|null
- */
- public static function getEnumNameByCode($code, array $data = [])
- {
- foreach ($data as $datum) {
- if (isset($datum['code']) && isset($datum['name']) && $datum['code'] === $code) {
- return $datum['name'];
- }
- }
- return null;
- }
- }
|