php按值对数组进行排序,对于相等的值,按键排序

deb*_*ish 2 php sorting array-multisort

我有一个阵列

(0=>0,1=>3,2=>3)
Run Code Online (Sandbox Code Playgroud)

我必须首先按值排序这个数组,对于相等的值,我必须先按增加键的顺序排序.

我试图分别在array_keys(SORT_DESC)和array_values(SORT_ASC)上使用multisort,但这给了我:

(0=>0,1=>3,2=>3)
Run Code Online (Sandbox Code Playgroud)

但我想要

(0=>0,2=>3,1=>3)
Run Code Online (Sandbox Code Playgroud)

Alm*_* Do 6

您始终可以使用简单的回调对其进行排序.使用uksort()它将是:

$input = array(0 => 0, 2 => 3, 1 => 3);

uksort($input, function($x, $y) use ($input)
{
   if($input[$x]==$input[$y])
   {
      return $x<$y?-1:$x!=$y;
   }
   return $input[$x]-$input[$y];
});
Run Code Online (Sandbox Code Playgroud)