如何整理以下数组?

时间:2018-07-26 14:19:05

标签: php arrays sorting

这是我的数组:

Array
(
    [angular] => 2
)
Array
(
    [android sdk] => 3
)
Array
(
    [application] => 1
)

下面是整理上面的数组的代码:

$weight = array($weight);
$dev = array_combine($missing, $weight);
arsort($dev);

foreach($dev as $x => $x_value) 
   echo "Key=" . $x . ", Value=" . $x_value;
   echo "<br>";

输出:

  

Key =角度,Value = 2 Key = android sdk,Value = 3 Key =应用程序,Value = 1

但是我希望结果按降序排列,如下所示:

  

Key = android sdk,Value = 3 Key = angular,Value = 2 Key = application,Value = 1

1 个答案:

答案 0 :(得分:0)

您想要的可能是an usort函数,您可以在其中提供自己的比较逻辑。

$weight = array($weight);
$dev = array_combine($missing, $weight);

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}


usort($dev, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}