thinkphp将名字按首字母进行排序
来源:互联网 发布:淘宝新娘饰品 编辑:程序博客网 时间:2024/06/05 19:43
一、获取每个名字姓的首字母
<span style="font-size:18px;">public function getFirstChar($s){ <span style="color:#FF0000;"> $s0 = mb_substr($s,0,1); </span> //获取名字的姓 $s = iconv('UTF-8','gb2312', $s0); //将UTF-8转换成GB2312编码 if (ord($s0)>128) { //汉字开头,汉字没有以U、V开头的 $asc=ord($s{0})*256+ord($s{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<=-17760)return "G"; if($asc>=-17759 and $asc<=-17248)return "H"; if($asc>=-17247 and $asc<=-17418)return "I"; 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"; }else if(ord($s)>=48 and ord($s)<=57){ //数字开头 switch(iconv_substr($s,0,1,'utf-8')){ case 1:return "Y"; case 2:return "E"; case 3:return "S"; case 4:return "S"; case 5:return "W"; case 6:return "L"; case 7:return "Q"; case 8:return "B"; case 9:return "J"; case 0:return "L"; } }else if(ord($s)>=65 and ord($s)<=90){ //大写英文开头 return substr($s,0,1); }else if(ord($s)>=97 and ord($s)<=122){ //小写英文开头 return strtoupper(substr($s,0,1)); } else { return iconv_substr($s0,0,1,'utf-8');//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可 } }
二、根据姓的首字母排序,并且生成相应的键值对
public function orderByName($userName){ $userName = array('张三','李四','王五','小二','猫蛋','狗蛋','王花','三毛','小明','Mary','李刚','张飞','Lucy'); sort($userName); foreach($userName as $name){$char = $this->getFirstChar($name);$nameArray = array();//将姓名按照姓的首字母与相对的首字母键进行配对if(count($charArray[$char])!=0){$nameArray = $charArray[$char];}array_push($nameArray,$name); $charArray[$char] = $nameArray;} echo '按首字母排序前:<br>; dump($charArray); ksort($charArray);//根据键值对排序 echo '按首字母排序后:<br>; dump($charArray); return $charArray;}
下面可以看到数组按照首字母排序前,排序后的区别:
三、通过html按照首字母显示名字数组:
<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>个人卡片</title></head><body><div class="body-right"> <ul class="select-A"> <?php $key = array_keys($charArray);//获取所有的键?> <foreach name="key" item="v"><li>{$v}</li> </foreach> </ul> <div class="select_B"> <table><?php $key = array_keys($charArray);//获取所有的键?><foreach name="key" item="v"><tr><?php $name = $charArray[$v];?><foreach name="name" item="v"><td>{$v}</td></foreach></tr></foreach> </table> </div></div></body></html>
就会在网页上看到下面的效果:
四、知识点拓展
(1)PHP 数组排序函数:
- sort() - 以升序对数组排序
- rsort() - 以降序对数组排序
- asort() - 根据值,以升序对关联数组进行排序
- ksort() - 根据键,以升序对关联数组进行排序
- arsort() - 根据值,以降序对关联数组进行排序
- krsort() - 根据键,以降序对关联数组进行排序
(2) array_push -- 将一个或多个单元压入数组的末尾(入栈)
例子:
<?php
$stack = array("orange","banana");
array_push($stack,"apple","raspberry");
print_r($stack);
?>
本例将使 $stack
具有如下单元:
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
(3) array_keys -- 返回数组中所有的键名
array_keys() 例子
<?php
$array = array(0=>100,"color"=>"red");
print_r(array_keys($array));
$array = array("blue","red","green","blue","blue");
print_r(array_keys($array,"blue"));
$array = array("color"=> array("blue","red","green"),
"size" => array("small","medium","large"));
print_r(array_keys($array));
?>
上例将输出:
Array( [0] => 0 [1] => color)Array( [0] => 0 [1] => 3 [2] => 4)Array( [0] => color [1] => size)
(4)获取字符串的第一个字符时,一般的都使用:$s0 = substr($s,0,1);
而为中文字符时,情况就不一样了,因为:utf-8 编码是变长的,中文不一定都是占3个字节,也有可能是两个字节。 最稳妥的办法就是用$s0 = mb_substr($s,0,1);
另外,在使用mb_substr()方法之前,请确定以下两点是否具备:
1.确保你的Windows/system32下有php_mbstring.dll这个文件,没有就从你Php安装目录extensions里拷入Windows/system32里面。
2.在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到
;extension=php_mbstring.dll把前面的;号去掉,这样mb_substr函数就可以生效了
mb_strcut函数功能也可以截取字符串长度,
1 0
- thinkphp将名字按首字母进行排序
- 07.将字符串按首字母排序
- android 中英混合字符串数组按首字母进行排序
- java丶对数组值按首字母进行排序
- php按首字母排序
- 汉字按首字母排序
- mysql按首字母排序
- java/android 对列表进行首字母排序
- iOS实现联系人按照首字母进行排序
- php将字符串按照首字母顺序排序
- 按照文件夹名字进行升序排序
- 将map按值进行排序
- sql按用户名首字母排序
- mysql按中文首字母排序
- Android 按中文首字母排序
- java按汉字首字母排序
- MYSQL按中文首字母排序
- MySQL按拼音首字母排序
- BP神经网络-- C语言实现 下
- MySql免安装版配置使用教程
- HDU 3364 Lanterns(高斯消元入门题目——开关问题)
- ADB安装与配置
- PHP通过curl向其它服务器发请求
- thinkphp将名字按首字母进行排序
- C#.net 通过后台 上传文件案例
- crontab不执行的原因和解决方案
- 演示Collection接口API介绍
- 18级的谷歌卫星地图纹理如何匹配到15级的高程
- Raptor实践参考:选择求值
- Java语言class类用法及泛化(详解)
- freeline集成
- 折半查找