$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; } }