PHP - sort hash array by key length

后端 未结 11 1371
星月不相逢
星月不相逢 2021-01-04 08:40

I\'ve found a few answers to sorting by value, but not key.

What I\'d like to do is a reverse sort, so with:

    $nametocode[\'reallylongname\']=\'12         


        
11条回答
  •  攒了一身酷
    2021-01-04 09:05

    One limitation while sorting the keys on the basis of length is that: equal length keys are not re-ordered. Say we need to order the keys by length in descending order.

    $arr = array(
        "foo 0" => "apple",
        "foo 1" => "ball",
        "foo 2 foo 0 foo 0" => "cat",
        "foo 2 foo 0 foo 1 foo 0" => "dog",
        "foo 2 foo 0 foo 1 foo 1" => "elephant",
        "foo 2 foo 1 foo 0" => "fish",
        "foo 2 foo 1 foo 1" => "giraffe"
    );
    
    debug($arr, "before sort");
    $arrBad = $arr;
    sortKeysDescBAD($arrBad);
    debug($arrBad, "after BAD sort");
    sortKeysDescGOOD($arr);
    debug($arr, "after GOOD sort 2");
    
    function sortKeysDescBAD(&$arrNew) {
        $arrKeysLength = array_map('strlen', array_keys($arrNew));
        array_multisort($arrKeysLength, SORT_DESC, $arrNew);
        //return max($arrKeysLength);
    }
    
    function sortKeysDescGOOD(&$arrNew) {
        uksort($arrNew, function($a, $b) {
            $lenA = strlen($a); $lenB = strlen($b);
            if($lenA == $lenB) {
                // If equal length, sort again by descending
                $arrOrig = array($a, $b);
                $arrSort = $arrOrig;
                rsort($arrSort);
                if($arrOrig[0] !== $arrSort[0]) return 1;
            } else {
                // If not equal length, simple
                return $lenB - $lenA;
            }
        });
    }
    
    function debug($arr, $title = "") {
        if($title !== "") echo "
    {$title}
    "; echo "
    "; print_r($arr); echo "

    "; }

    Output will be:

    before sort
    Array
    (
        [foo 0] => apple
        [foo 1] => ball
        [foo 2 foo 0 foo 0] => cat
        [foo 2 foo 0 foo 1 foo 0] => dog
        [foo 2 foo 0 foo 1 foo 1] => elephant
        [foo 2 foo 1 foo 0] => fish
        [foo 2 foo 1 foo 1] => giraffe
    )
    
    after BAD sort
    Array
    (
        [foo 2 foo 0 foo 1 foo 0] => dog
        [foo 2 foo 0 foo 1 foo 1] => elephant
        [foo 2 foo 0 foo 0] => cat
        [foo 2 foo 1 foo 0] => fish
        [foo 2 foo 1 foo 1] => giraffe
        [foo 0] => apple
        [foo 1] => ball
    )
    
    after GOOD sort
    Array
    (
        [foo 2 foo 0 foo 1 foo 1] => elephant
        [foo 2 foo 0 foo 1 foo 0] => dog
        [foo 2 foo 1 foo 1] => giraffe
        [foo 2 foo 1 foo 0] => fish
        [foo 2 foo 0 foo 0] => cat
        [foo 1] => ball
        [foo 0] => apple
    )
    

    Notice the order of elephant and dog for example (or others) in two sorting methods. The second method looks better. There may be easier ways to solve this but hope this helps someone...

提交回复
热议问题