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

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

问题描述

我在这个格式的数组:

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天全站免登陆