如何整理以下数组? [英] How to arsort the following array?

查看:86
本文介绍了如何整理以下数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组:

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

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

Here is the code to arsort the array above:

$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=angular, Value=2 Key=android sdk, Value=3 Key=application, Value=1

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

But I'd like the result to be in descending order, like below:

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

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

推荐答案

您想要的可能是

What you want might be an usort function, where you can provide your own comparison logic.

$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";
}

这篇关于如何整理以下数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆