Util.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yckj-yf7
  5. * Date: 2021/9/24
  6. * Time: 17:25
  7. */
  8. namespace app\common\util;
  9. /**
  10. * 工具类
  11. * @package app\common\util
  12. */
  13. class Util
  14. {
  15. /**
  16. * 获取一定配置下的父子级的数据集合
  17. * @param $list 一维的数据集合
  18. * @param array $option 配置
  19. * field 判断子级的字段名,默认为id
  20. * parent_field 判断父级的字段名,默认为pid
  21. * children 写入返回值的字段名,默认为children
  22. * default 默认搜索开始的数值,默认为0
  23. * @return array|mixed
  24. */
  25. public static function getChildrenList($list, $option = []){
  26. $field = isset($option['field'])?$option['field']:'id';
  27. $parent_field = isset($option['parent_field'])?$option['parent_field']:'pid';
  28. $children_name = isset($option['children'])?$option['children']:'children';
  29. $default = isset($option['default'])?$option['default']:0;
  30. //按父级生成索引数组
  31. $index_data = [];
  32. foreach ($list as $item){
  33. $index_data[$item[$parent_field]][] = $item;
  34. }
  35. unset($item);
  36. return self::setChildren($index_data,$default,$field,$children_name);
  37. }
  38. /**
  39. * 写入子集
  40. * @param $index_data 索引数组
  41. * @param $pid 父级数值
  42. * @param $field 关联字段名
  43. * @param $children_name 写入的子集字段名
  44. * @return array|mixed
  45. */
  46. private static function setChildren($index_data, $pid, $field, $children_name){
  47. if (isset($index_data[$pid])){
  48. $list = $index_data[$pid];
  49. foreach ($list as &$item){
  50. $children = self::setChildren($index_data,$item[$field],$field, $children_name);
  51. if (count($children)>0){
  52. $item[$children_name] = $children;
  53. }
  54. }
  55. return $list;
  56. }else{
  57. return [];
  58. }
  59. }
  60. /**
  61. * 获取两个日期之间的日期数组
  62. * @param $start_time
  63. * @param $end_time
  64. * @return mixed
  65. */
  66. public static function getPeriodDate($start_time,$end_time){
  67. $start_time = strtotime($start_time);
  68. $end_time = strtotime($end_time);
  69. $i = 0;
  70. $arr = [];
  71. while ($start_time <= $end_time){
  72. $arr[$i] = date('Y-m-d',$start_time);
  73. $start_time = strtotime('+1 day',$start_time);
  74. $i++;
  75. }
  76. return $arr;
  77. }
  78. /**
  79. * 根据数字转换成excel的字母
  80. * @param $num
  81. * @return string
  82. */
  83. public static function numToExcelLetter($num)
  84. {
  85. //由于大写字母只有26个,所以基数为26
  86. $base = 26;
  87. $result = '';
  88. while ($num > 0 ) {
  89. $mod = (int)($num % $base);
  90. $num = (int)($num / $base);
  91. if($mod == 0){
  92. $num -= 1;
  93. $temp = self::numToLetter($base) . $result;
  94. } elseif ($num == 0) {
  95. $temp = self::numToLetter($mod) . $result;
  96. } else {
  97. $temp = self::numToLetter($mod) . $result;
  98. }
  99. $result = $temp;
  100. }
  101. return $result;
  102. }
  103. /**
  104. * 数字转字母
  105. * @param $num
  106. * @return string
  107. */
  108. public static function numToLetter($num)
  109. {
  110. if ($num == 0) {
  111. return '';
  112. }
  113. $num = (int)$num - 1;
  114. //获取A的ascii码
  115. $ordA = ord('A');
  116. return chr($ordA + $num);
  117. }
  118. }