* @since 2.0 */ class ArrayHelper extends \yii\helpers\ArrayHelper { /** * ------------------------------------------ * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pk 主键 * @param string $pid parent标记字段 * @param string $child * @param int $root * @return array * ------------------------------------------ */ public static function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) { // 创建Tree $tree = []; if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; }else{ if (isset($refer[$parentId])) { $parent =& $refer[$parentId]; $parent[$child][] =& $list[$key]; } } } } return $tree; } /** * --------------------------------------------------- * 将list_to_tree的树还原成列表 * @param array $tree 原来的树 * @param string $child 孩子节点的键 * @param string $order 排序显示的键,一般是主键 升序排列 * @param array $list 过渡用的中间数组, * @return array 返回排过序的列表数组 * --------------------------------------------------- */ public static function tree_to_list($tree, $child = '_child', $order='id', &$list = []){ if(is_array($tree)) { $refer = []; foreach ($tree as $key => $value) { $reffer = $value; if(isset($reffer[$child])){ unset($reffer[$child]); static::tree_to_list($value[$child], $child, $order, $list); } $list[] = $reffer; } $list = static::list_sort_by($list, $order, $sortby='asc'); } return $list; } /** * -------------------------------------------------- * 对查询结果集进行排序 * @access public * @param array $list 查询结果 * @param string $field 排序的字段名 * @param string $sortby 排序类型 asc正向排序 desc逆向排序 nat自然排序 * @return array|boolean * -------------------------------------------------- */ public static function list_sort_by($list, $field, $sortby = 'asc') { if(is_array($list)){ $refer = $resultSet = array(); foreach ($list as $i => $data) $refer[$i] = &$data[$field]; switch ($sortby) { case 'asc': // 正向排序 asort($refer); break; case 'desc':// 逆向排序 arsort($refer); break; case 'nat': // 自然排序 natcasesort($refer); break; } foreach ( $refer as $key=> $val) $resultSet[] = &$list[$key]; return $resultSet; } return false; } /** * --------------------------------------- * 递归方式将tree结构转化为 表单中select可使用的格式 * @param array $tree 树型结构的数组 * @param string $title 将格式化的字段 * @param int $level 当前循环的层次,从0开始 * @return array * --------------------------------------- */ public static function format_tree($tree, $title = 'title', $level = 0){ static $list; /* 按层级格式的字符串 */ $tmp_str=str_repeat("  ",$level)."└"; $level == 0 && $tmp_str = ''; foreach ($tree as $key => $value) { $value[$title] = $tmp_str.$value[$title]; $arr = $value; if (isset($arr['_child'])) unset($arr['_child']); $list[] = $arr; if (array_key_exists('_child', $value)) { static::format_tree($value['_child'], $title, $level+1); } } return $list; } /** * --------------------------------------- * 获取dropDownList的data数据,主要是二级栏目及以上数据,一级栏目可以用ArrayHelper::map()生成 * 示例:ArrayHelper::listDataLevel(\backend\models\Menu::find()->asArray()->all(), 'id', 'title', 'id', 'pid') * @param $list array 由findAll或->all()生成的数据 * @param $key string dropDownList的data数据的key * @param $value string dropDownList的data数据的value * @param string $pk 主键字段名 * @param string $pid 父id字段名 * @param int $root 根ID * @return array * --------------------------------------- */ public static function listDataLevel($list, $key, $value, $pk = 'id', $pid = 'pid', $root = 0){ if (!is_array($list)) { return []; } $_tmp = $list; /* 判断$list是否由findAll生成的数据 */ if (array_shift($_tmp) instanceof \yii\base\Model) { $list = array_map(function($record) {return $record->attributes;},$list); } unset($_tmp); $tree = static::list_to_tree($list,$pk,$pid,'_child',$root); return static::map( static::format_tree($tree, $value), $key, $value); } /** * --------------------------------------- * 生成jQuery tree所需的数据 * @param $list array 由self::list_to_tree生成的数据 * @return array * --------------------------------------- */ public static function jstree($list){ $node = []; if ($list) { foreach ($list as $value) { $_tmp = []; $_tmp['id'] = $value['id']; $_tmp['text'] = $value['title']; if (isset($value['_child'])) { $_tmp['icon'] = 'fa fa-folder icon-state-warning'; $_tmp['state']['opened'] = true; $_tmp['children'] = self::jstree($value['_child']); } else { $_tmp['icon'] = 'fa fa-file icon-state-warning'; } $node[] = $_tmp; } } return $node; } /** * 递归数组 * * @param array $items * @param string $idField * @param int $pid * @param string $pidField * @return array */ public static function itemsMerge(array $items, $pid = 0, $idField = "id", $pidField = 'pid', $child = '-') { $map = []; $tree = []; foreach ($items as &$it) { //$it[$child] = []; $map[$it[$idField ]] = &$it; } foreach ($items as &$it) { $parent = &$map[$it[$pidField]]; if ($parent) { $parent[$child][] = &$it; } else { $pid == $it[$pidField] && $tree[] = &$it; } } unset($items, $map); return $tree; } public static function shouzimu($name) { $ret = ""; $s1 = iconv("UTF-8", "gb2312", $name); $s2 = iconv("gb2312", "UTF-8", $s1); if ($s2 == $name) { $post['name'] = $s1; } for ($i = 0; $i < strlen($name); $i++) { $s1 = substr($post['name'], $i, 1); $p = ord($s1); if ($p > 160) { $s2 = substr($post['name'], $i++, 2); $ret .= ArrayHelper::ff_letter_first($s2); } else { $ret .= $s1; } } return $ret; } //生成字母前缀 public static function ff_letter_first($s0){ // if (empty($s0)) { // return ''; // } // //取出参数字符串中的首个字符 // $temp_str = substr($s0, 0, 1); // if (ord($temp_str) > 127) { // $str = substr($s0, 0, 3); // } else { // $str = $temp_str; // $fchar = ord($str[0]); // if ($fchar >= ord('A') && $fchar <= ord('z')) { // return strtoupper($temp_str); // } else { // return null; // } // } // $s1 = iconv('UTF-8', 'gbk', $str); // if (empty($s1)) { // return null; // } // $s2 = iconv('gbk', 'UTF-8', $s1); // if (empty($s2)) { // return null; // } // $s = $s2 == $str ? $s1 : $str; // $asc = ord($s[0]) * 256 + ord($s[1]) - 65536; $firstchar_ord=ord(strtoupper($s0[0])); if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)){ $s0=iconv("UTF-8","gb2312//IGNORE", $s0); } $asc=ord($s0[0])*256+ord($s0[1])-65536; if($asc>=-20319 and $asc<=-20284)return "A"; if($asc>=-20283 and $asc<=-19776)return "B"; if($asc>=-19775 and $asc<=-19219)return "C"; if($asc>=-19218 and $asc<=-18711)return "D"; if($asc>=-18710 and $asc<=-18527)return "E"; if($asc>=-18526 and $asc<=-18240)return "F"; if($asc>=-18239 and $asc<=-17923)return "G"; if($asc>=-17922 and $asc<=-17418)return "H"; if($asc>=-17417 and $asc<=-16475)return "J"; if($asc>=-16474 and $asc<=-16213)return "K"; if($asc>=-16212 and $asc<=-15641)return "L"; if($asc>=-15640 and $asc<=-15166)return "M"; if($asc>=-15165 and $asc<=-14923)return "N"; if($asc>=-14922 and $asc<=-14915)return "O"; if($asc>=-14914 and $asc<=-14631)return "P"; if($asc>=-14630 and $asc<=-14150)return "Q"; if($asc>=-14149 and $asc<=-14091)return "R"; if($asc>=-14090 and $asc<=-13319)return "S"; if($asc>=-13318 and $asc<=-12839)return "T"; if($asc>=-12838 and $asc<=-12557)return "W"; if($asc>=-12556 and $asc<=-11848)return "X"; if($asc>=-11847 and $asc<=-11056)return "Y"; if($asc>=-11055 and $asc<=-10247)return "Z"; return self::rare_words($asc); } /** * 百家姓中的生僻字 */ public static function rare_words($asc = '') { $rare_arr = array( -3652 => array('word' => "窦", 'first_char' => 'D'), -8503 => array('word' => "奚", 'first_char' => 'X'), -9286 => array('word' => "酆", 'first_char' => 'F'), -7761 => array('word' => "岑", 'first_char' => 'C'), -5128 => array('word' => "滕", 'first_char' => 'T'), -9479 => array('word' => "邬", 'first_char' => 'W'), -5456 => array('word' => "臧", 'first_char' => 'Z'), -7223 => array('word' => "闵", 'first_char' => 'M'), -2877 => array('word' => "裘", 'first_char' => 'Q'), -6191 => array('word' => "缪", 'first_char' => 'M'), -5414 => array('word' => "贲", 'first_char' => 'B'), -4102 => array('word' => "嵇", 'first_char' => 'J'), -8969 => array('word' => "荀", 'first_char' => 'X'), -4938 => array('word' => "於", 'first_char' => 'Y'), -9017 => array('word' => "芮", 'first_char' => 'R'), -2848 => array('word' => "羿", 'first_char' => 'Y'), -9477 => array('word' => "邴", 'first_char' => 'B'), -9485 => array('word' => "隗", 'first_char' => 'K'), -6731 => array('word' => "宓", 'first_char' => 'M'), -9299 => array('word' => "郗", 'first_char' => 'X'), -5905 => array('word' => "栾", 'first_char' => 'L'), -4393 => array('word' => "钭", 'first_char' => 'T'), -9300 => array('word' => "郜", 'first_char' => 'G'), -8706 => array('word' => "蔺", 'first_char' => 'L'), -3613 => array('word' => "胥", 'first_char' => 'X'), -8777 => array('word' => "莘", 'first_char' => 'S'), -6708 => array('word' => "逄", 'first_char' => 'P'), -9302 => array('word' => "郦", 'first_char' => 'L'), -5965 => array('word' => "璩", 'first_char' => 'Q'), -6745 => array('word' => "濮", 'first_char' => 'P'), -4888 => array('word' => "扈", 'first_char' => 'H'), -9309 => array('word' => "郏", 'first_char' => 'J'), -5428 => array('word' => "晏", 'first_char' => 'Y'), -2849 => array('word' => "暨", 'first_char' => 'J'), -7206 => array('word' => "阙", 'first_char' => 'Q'), -4945 => array('word' => "殳", 'first_char' => 'S'), -9753 => array('word' => "夔", 'first_char' => 'K'), -10041 => array('word' => "厍", 'first_char' => 'S'), -5429 => array('word' => "晁", 'first_char' => 'C'), -2396 => array('word' => "訾", 'first_char' => 'Z'), -7205 => array('word' => "阚", 'first_char' => 'K'), -10049 => array('word' => "乜", 'first_char' => 'N'), -10015 => array('word' => "蒯", 'first_char' => 'K'), -3133 => array('word' => "竺", 'first_char' => 'Z'), -6698 => array('word' => "逯", 'first_char' => 'L'), -9799 => array('word' => "俟", 'first_char' => 'Q'), -6749 => array('word' => "澹", 'first_char' => 'T'), -7220 => array('word' => "闾", 'first_char' => 'L'), -10047 => array('word' => "亓", 'first_char' => 'Q'), -10005 => array('word' => "仉", 'first_char' => 'Z'), -3417 => array('word' => "颛", 'first_char' => 'Z'), -6431 => array('word' => "驷", 'first_char' => 'S'), -7226 => array('word' => "闫", 'first_char' => 'Y'), -9293 => array('word' => "鄢", 'first_char' => 'Y'), -6205 => array('word' => "缑", 'first_char' => 'G'), -9764 => array('word' => "佘", 'first_char' => 'S'), -9818 => array('word' => "佴", 'first_char' => 'N'), -9509 => array('word' => "谯", 'first_char' => 'Q'), -3122 => array('word' => "笪", 'first_char' => 'D'), -9823 => array('word' => "佟", 'first_char' => 'T'), ); if (array_key_exists($asc, $rare_arr) && $rare_arr[$asc]['first_char']) { return $rare_arr[$asc]['first_char']; } else { return null; } } }