PHP - sort hash array by key length
问题 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'; $nametocode['shortname']='10'; $nametocode['mediumname']='11'; I'd like them to be in this order reallylongname mediumname shortname mediumname shortname Many thanks 回答1: Another solution using array_multisort: $keys = array_map('strlen', array_keys($arr)); array_multisort($keys, SORT_DESC, $arr); Here $keys is an array of the lengths of the keys of