PHP - sort hash array by key length

后端 未结 11 1215
星月不相逢
星月不相逢 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:12

    In PHP7+ you can use uksort() with spaceship operator and anonymous function like this:

    uksort($array, function($a, $b) {
        return strlen($b) <=> strlen($a);
    });
    

提交回复
热议问题