且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP按字母顺序排序数组,然后按数字排序?

更新时间:2023-02-06 13:49:28

您需要的是排序但具有自定义比较功能(usort). 下面的代码可以完成它:

What you need is sort but with a custom comparison function (usort). The following code will get it done:

function myComparison($a, $b){
    if(is_numeric($a) && !is_numeric($b))
        return 1;
    else if(!is_numeric($a) && is_numeric($b))
        return -1;
    else
        return ($a < $b) ? -1 : 1;
} 
$test = array("def", "yz", "abc", "jkl", "123", "789", "stu");
usort ( $test , 'myComparison' );