在 PHP 中对关联数组进行排序 [英] Sorting an associative array in PHP

查看:33
本文介绍了在 PHP 中对关联数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的数组:

I have an array in this format:

Array
(
    [0] => Array
        (
            [text] => tests
            [language] => 
            [advertiserCompetitionScale] => 5
            [avgSearchVolume] => 7480000
            [lastMonthSearchVolume] => 9140000
        )

    [1] => Array
        (
            [text] => personality tests
            [language] => 
            [advertiserCompetitionScale] => 5
            [avgSearchVolume] => 165000
            [lastMonthSearchVolume] => 201000
        )

    [2] => Array
        (
            [text] => online tests
            [language] => 
            [advertiserCompetitionScale] => 5
            [avgSearchVolume] => 246000
            [lastMonthSearchVolume] => 301000
        )

)

如何按照 avgSearchVolume 字段的降序对这种格式的数组进行排序?有内置函数吗?

How can I sort an array in that format, in the descending order of the avgSearchVolume field? Is there a built in function for this?

推荐答案

使用 usort 并提供您自己的功能来进行排序,例如

Use usort and supply your own function to do the ordering, e.g.

function cmp($a, $b)
{
    return $b['avgSearchVolume'] - $a['avgSearchVolume'];
}

usort($array, "cmp");

这篇关于在 PHP 中对关联数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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