DataUtil.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\utils;
  3. class DataUtil
  4. {
  5. public static $_treeArr = [];
  6. /**
  7. * 递归
  8. * @param array $array 数据
  9. * @param string $index key
  10. * @param string $pid 父级key
  11. *
  12. * @return array
  13. */
  14. public static function recursion($array, $index = 'id', $pid = 'parent_id')
  15. {
  16. //第一步 构造数据
  17. $items = [];
  18. if(empty($array))
  19. {
  20. return [];
  21. }
  22. foreach($array as $value)
  23. {
  24. $items[$value[$index]] = $value;
  25. }
  26. //第二部 遍历数据 生成树状结构
  27. $tree = array();
  28. foreach($items as $key => $item)
  29. {
  30. if(isset($items[$item[$pid]]))
  31. {
  32. $items[$item[$pid]]['children'][] = &$items[$key];
  33. }else{
  34. $tree[] = &$items[$key];
  35. }
  36. }
  37. return $tree;
  38. }
  39. public static function threeMenus($array, $index = 'id', $pid = 'parent_id'){
  40. //第一步 构造数据
  41. $items = [];
  42. if(empty($array))
  43. {
  44. return [];
  45. }
  46. foreach($array as $value)
  47. {
  48. if ($value[$pid] == 0) {
  49. $value['open'] = true;
  50. }else{
  51. $value['open'] = false;
  52. }
  53. $value['spread'] = true;
  54. $items[$value[$index]] = $value;
  55. }
  56. //第二部 遍历数据 生成树状结构
  57. $tree = array();
  58. foreach($items as $key => $item)
  59. {
  60. if(isset($items[$item[$pid]]))
  61. {
  62. $items[$item[$pid]]['children'][] = &$items[$key];
  63. }else{
  64. $tree[] = &$items[$key];
  65. }
  66. }
  67. return $tree;
  68. }
  69. public static function roleThreeMenus(array $array,array $menu_ids = [], $index = 'id', $pid = 'parent_id'){
  70. //第一步 构造数据
  71. $items = [];
  72. if(empty($array))
  73. {
  74. return [];
  75. }
  76. $ids = [];
  77. foreach ($array as $k=>$v){
  78. if (empty($v[$pid])) continue;
  79. $ids[] = $v[$pid];
  80. }
  81. foreach($array as $value)
  82. {
  83. //设置最后一级默认选中
  84. if (!in_array($value[$index],$ids) && in_array($value[$index],$menu_ids)) $value['checked'] = true;
  85. $value['spread'] = true;
  86. $items[$value[$index]] = $value;
  87. }
  88. //第二部 遍历数据 生成树状结构
  89. $tree = array();
  90. foreach($items as $key => $item)
  91. {
  92. if(isset($items[$item[$pid]]))
  93. {
  94. $items[$item[$pid]]['children'][] = &$items[$key];
  95. }else{
  96. $tree[] = &$items[$key];
  97. }
  98. }
  99. return $tree;
  100. }
  101. public static function arrayColumnKey(array $items, $index = 'id'){
  102. if(empty($items))
  103. return [];
  104. $items_arr = [];
  105. foreach ($items as $val)
  106. {
  107. if(!isset($val[$index]))
  108. return [];
  109. $items_arr[$val[$index]] = $val;
  110. }
  111. unset($items);
  112. return $items_arr;
  113. }
  114. /**
  115. * stringToArray
  116. * 把字符串转换为数组
  117. *
  118. * @param string $string 需要处理的字符串
  119. * @access common
  120. * @since 1.0
  121. * @return array
  122. */
  123. public static function stringToArray($string){
  124. /*以逗号切割字符串;转换成数组*/
  125. if (empty($string)) return [];
  126. $items = explode(',', $string);
  127. if(empty($items)){
  128. return [];
  129. }
  130. /*处理数组 先删除数组的空元素 去除重复值, 然后value数组,使数组的key保持统一*/
  131. $items = array_values(array_unique(array_filter($items)));
  132. if(empty($items)){
  133. return [];
  134. }
  135. return $items;
  136. }
  137. /**
  138. * 表单字段为空初始化
  139. * 注意,仅用于类似后台
  140. * @param array $fields
  141. * @param array $unset_fields 销毁指定字段
  142. * @return array
  143. */
  144. public static function field(array $fields,array $unset_fields)
  145. {
  146. foreach ($fields as $key => $val){
  147. if ($val == 'null' || $val == null || $val == 'undefined'){
  148. unset($fields[$key]);
  149. }
  150. // 销毁指定字段
  151. if (in_array($key,$unset_fields)){
  152. unset($fields[$key]);
  153. }
  154. }
  155. return $fields;
  156. }
  157. }