源码

首页 » 归档 » 源码 » PHP – 更改字符排序顺序,例如“a”“{”…

PHP – 更改字符排序顺序,例如“a”“{”…


我正在尝试自定义PHP的usort函数来更改字符排序顺序.

目前,只有“*”符号字符等于具有小于字母字符的值,例如“a”,即“*”< “a”= TRUE.其他符号,例如“{”,具有大于字母的值,例如“a”,即“{”< “a”=假. 我想排序所以值“{*}”位于排序数组的顶部,就像值是“*”一样.这是我目前正在使用的函数,用于按对象的多个属性对对象数组进行排序. [归因:它是usort docsWill Shaver’s代码的修改版本.]

function osort($array, $properties) {
    //Cast to an array and specify ascending order for the sort if the properties aren't already
    if (!is_array($properties)) $properties = [$properties => true];

    //Run the usort, using an anonymous function / closures
    usort($array, function($a, $b) use ($properties) {
        //Loop through each specified object property to sort by
        foreach ($properties as $property => $ascending) {
            //If they are the same, continue to the next property
            if ($a -> $property != $b -> $property) {
                //Ascending order search for match
                if ($ascending) {
                    //if a's property is greater than b, return 1, otherwise -1
                    return $a -> $property > $b -> $property ? 1 : -1;
                }
                //Descending order search for match
                else {
                    //if b's property is greater than a's, return 1, otherwise -1
                    return $b -> $property > $a -> $property ? 1 : -1;
                }
            }
        }
        //Default return value (no match found)
        return -1;
    });

    //Return the sorted array
    return $array;
}
(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/php-genggaizifupaixushunxulirua.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:11 月 11, 2019 at 06:37 下午

热评文章