ArrayUtil.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace app\utils;
  3. class ArrayUtil
  4. {
  5. /**
  6. * 将数组内的 keys下划线转驼峰
  7. *
  8. * @param array $arr
  9. * @param string $separator
  10. *
  11. * @return array
  12. */
  13. public static function underline2CamelArr(array $arr, string $separator = '_'): array
  14. {
  15. $result = [];
  16. if (!empty($arr)) {
  17. foreach ($arr as $words => $value) {
  18. if (strpos($words, $separator) !== false) {
  19. $words = $separator . str_replace($separator, " ", strtolower($words));
  20. $result[ltrim(str_replace(" ", "", ucwords($words)), $separator)] = $value;
  21. } else {
  22. $result[$words] = $value;
  23. }
  24. }
  25. }
  26. return $result;
  27. }
  28. /**
  29. * 根据 code 获取 name
  30. * @param $code
  31. * @param array $data
  32. * @return mixed|null
  33. */
  34. public static function getEnumNameByCode($code, array $data = [])
  35. {
  36. foreach ($data as $datum) {
  37. if (isset($datum['code']) && isset($datum['name']) && $datum['code'] === $code) {
  38. return $datum['name'];
  39. }
  40. }
  41. return null;
  42. }
  43. }