Access array key using uasort in PHP

前端 未结 2 1914
抹茶落季
抹茶落季 2020-12-31 13:25

If have a rather basic uasort function in PHP that looks like this:

uasort($arr, function($a, $b) {
                if ($a > $b)
                     


        
2条回答
  •  醉话见心
    2020-12-31 14:05

    uksort($arr, function ($a, $b) use ($arr) {
        return $arr[$a] - $arr[$b] ?: $a - $b;
    });
    

    You can get the values by the keys, so use uksort which gives you the keys. Replace $a - $b with your appropriate magic, here it's just sorting by the key's value.

提交回复
热议问题