文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>如何用PHP4进行自然排序

如何用PHP4进行自然排序

时间:2007-02-17  来源:PHP爱好者

对于这样几个字串:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf1
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf3
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf12
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
如果我们只是用strcmp()函数来做比较,得出的次序如下:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf1
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf12
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
inf3
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
可这显然不是我们想要的结果,一般我们的解决方法是采用在数字前加一个0,如 inf01 inf03 inf12 这样。可不可以不用加这个0也能按自然次序(nature order)排出我们想要的真正的顺序呢?
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
在PHP4中提供了这样两个函数可以做到这一点:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
int strnatcasecmp (string str1, string str2)
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
int strnatcmp(string str1, string str2)
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
它们支持的版本是:PHP4 >= 4.0RC2
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
说明如下:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
按自然次序(nature order)对 str1 str2 进行排序。
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
返回值 <0 表示 str1 小于 str2,
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
返回值 =0 表示 str1 等于 str2,
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
返回值 >0 表示 str1 大于 str2。
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
例子如下:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
<?
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$str=array("a","a0","a3","a2","a10","a18","a24");
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$k=count($str);
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
echo "total=".$k."<br><br>";
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
echo "<br>用strnatcmp排序<br>";
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
for($i=1;$i<$k;$i++){
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
if(strnatcmp($str[$i-1],$str[$i])>0){
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$tmp=$str[$i-1];
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$str[$i-1]=$str[$i];
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$str[$i]=$tmp;
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
}
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
}
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
print_r($str);
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
echo "<br>用strcmp排序<br>";
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
for($i=1;$i<$k;$i++){
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
if(strcmp($str[$i-1],$str[$i])>0){
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$tmp=$str[$i-1];
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$str[$i-1]=$str[$i];
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
$str[$i]=$tmp;
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
}
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
}
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
print_r($str);
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
echo "<br>用sort排序<br>";
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
sort($str);
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
print_r($str);
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
?>
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
输出如下:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
total=7
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
用strnatcmp排序
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
Array ( [0] => a [1] => a0 [2] => a2 [3] => a3 [4] => a10 [5] => a18 [6] => a24 )
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
用strcmp排序
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
Array ( [0] => a [1] => a0 [2] => a2 [3] => a10 [4] => a18 [5] => a24 [6] => a3 )
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
用sort排序
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
Array ( [0] => a [1] => a0 [2] => a10 [3] => a18 [4] => a2 [5] => a24 [6] => a3 )
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
明白了吗?
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
其实这个函数也支持比较复杂一点的功能。如:
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
a <a0 <a1 <a1a <a1b <a2 <a10 <a20
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
x2-g8 <x2-y7 <x2-y08 <x8-y8
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
1.001 <1.002 <1.010 <1.02 <1.1 <1.3
www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o

www.c hinaitpower.comlMyb83oas7CufhmSPWQOCXMs9o
试一下吧,真得很有用。
非常全面的一个php技术网站,php爱好者站 http://www.phpfans.net 有相当丰富的文章和源代码.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载