DeTechn Blog

['a'=>200,'b'=>100,'c'=>100],写一个自定义排序函数,按值降序,如果值一样,按键排序

function kvsort($arr) {
    $res_arr = [];
    while (count($arr)) {
        $min = null;
        $min_key = null;

        foreach ($arr as $key=>$value) {
            if(!$min || $min > $value) {
                $min = $value;
                $min_key = $key;
            } else if($min == $value && $min_key > $key) {
                $min = $value;
                $min_key = $key;
            }
        }
        unset($arr[$min_key]);
        $res_arr[$min_key] = $min;
    }
    return $res_arr;
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »