123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\utils;
- class DataUtil
- {
- public static $_treeArr = [];
- /**
- * 递归
- * @param array $array 数据
- * @param string $index key
- * @param string $pid 父级key
- *
- * @return array
- */
- public static function recursion($array, $index = 'id', $pid = 'parent_id')
- {
- //第一步 构造数据
- $items = [];
- if(empty($array))
- {
- return [];
- }
- foreach($array as $value)
- {
- $items[$value[$index]] = $value;
- }
- //第二部 遍历数据 生成树状结构
- $tree = array();
- foreach($items as $key => $item)
- {
- if(isset($items[$item[$pid]]))
- {
- $items[$item[$pid]]['children'][] = &$items[$key];
- }else{
- $tree[] = &$items[$key];
- }
- }
- return $tree;
- }
- public static function threeMenus($array, $index = 'id', $pid = 'parent_id'){
- //第一步 构造数据
- $items = [];
- if(empty($array))
- {
- return [];
- }
- foreach($array as $value)
- {
- if ($value[$pid] == 0) {
- $value['open'] = true;
- }else{
- $value['open'] = false;
- }
- $value['spread'] = true;
- $items[$value[$index]] = $value;
- }
- //第二部 遍历数据 生成树状结构
- $tree = array();
- foreach($items as $key => $item)
- {
- if(isset($items[$item[$pid]]))
- {
- $items[$item[$pid]]['children'][] = &$items[$key];
- }else{
- $tree[] = &$items[$key];
- }
- }
- return $tree;
- }
- public static function roleThreeMenus(array $array,array $menu_ids = [], $index = 'id', $pid = 'parent_id'){
- //第一步 构造数据
- $items = [];
- if(empty($array))
- {
- return [];
- }
- $ids = [];
- foreach ($array as $k=>$v){
- if (empty($v[$pid])) continue;
- $ids[] = $v[$pid];
- }
- foreach($array as $value)
- {
- //设置最后一级默认选中
- if (!in_array($value[$index],$ids) && in_array($value[$index],$menu_ids)) $value['checked'] = true;
- $value['spread'] = true;
- $items[$value[$index]] = $value;
- }
- //第二部 遍历数据 生成树状结构
- $tree = array();
- foreach($items as $key => $item)
- {
- if(isset($items[$item[$pid]]))
- {
- $items[$item[$pid]]['children'][] = &$items[$key];
- }else{
- $tree[] = &$items[$key];
- }
- }
- return $tree;
- }
- public static function arrayColumnKey(array $items, $index = 'id'){
- if(empty($items))
- return [];
- $items_arr = [];
- foreach ($items as $val)
- {
- if(!isset($val[$index]))
- return [];
- $items_arr[$val[$index]] = $val;
- }
- unset($items);
- return $items_arr;
- }
- /**
- * stringToArray
- * 把字符串转换为数组
- *
- * @param string $string 需要处理的字符串
- * @access common
- * @since 1.0
- * @return array
- */
- public static function stringToArray($string){
- /*以逗号切割字符串;转换成数组*/
- if (empty($string)) return [];
- $items = explode(',', $string);
- if(empty($items)){
- return [];
- }
- /*处理数组 先删除数组的空元素 去除重复值, 然后value数组,使数组的key保持统一*/
- $items = array_values(array_unique(array_filter($items)));
- if(empty($items)){
- return [];
- }
- return $items;
- }
- /**
- * 表单字段为空初始化
- * 注意,仅用于类似后台
- * @param array $fields
- * @param array $unset_fields 销毁指定字段
- * @return array
- */
- public static function field(array $fields,array $unset_fields)
- {
- foreach ($fields as $key => $val){
- if ($val == 'null' || $val == null || $val == 'undefined'){
- unset($fields[$key]);
- }
- // 销毁指定字段
- if (in_array($key,$unset_fields)){
- unset($fields[$key]);
- }
- }
- return $fields;
- }
- }
|