年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1879|回复: 0

PHP智能自然排序natsort()函数

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2015-12-30 18:01:09 | 显示全部楼层 |阅读模式
    [PHP] 纯文本查看 复制代码
    <?php
    	
    	/*
    	 * natsort:自然排序,根据字母与数字的组合方式进行排序比较
    	 * sort()函数中 file14 < file2,因为第4位前者1小于后者2
    	 * natcasesort():忽略大小写
    	 */
    	$array = array("file.txt", "File1.txt", "file14.txt", "file13.txt", "file2.txt");
    	
    	print_r($array);
    	echo "<br/>";
    	natsort($array);
    	print_r($array);
    	echo "<br/>";
    	natcasesort($array);
    	print_r($array);
    	echo "<br/>";

    输出结果:
    Array ( [0] => file.txt [1] => File1.txt [2] => file14.txt [3] => file13.txt [4] => file2.txt )
    Array ( [1] => File1.txt [0] => file.txt [4] => file2.txt [3] => file13.txt [2] => file14.txt )
    Array ( [0] => file.txt [1] => File1.txt [4] => file2.txt [3] => file13.txt [2] => file14.txt )
    Array ( [0] => file.txt [1] => File1.txt [4] => file2.txt [3] => file13.txt [2] => file14.txt )
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 18:17 , Processed in 0.043383 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表