一、获取每个名字姓的首字母
<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 |
(3) array_keys -- 返回数组中所有的键名
array_keys() 例子
<?php |
上例将输出:
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函数功能也可以截取字符串长度,