在PHP中对关联数组进行排序

时间:2009-04-22 14:48:26

标签: php arrays sorting

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

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字段的降序对该格式的数组进行排序?是否有内置功能?

6 个答案:

答案 0 :(得分:102)

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

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

usort($array, "cmp");

答案 1 :(得分:18)

在PHP 5.3之前,这是基于子键进行排序的最佳功能,而不为每个键创建新功能。

function sortBySubkey(&$array, $subkey, $sortType = SORT_ASC) {
    foreach ($array as $subarray) {
        $keys[] = $subarray[$subkey];
    }
    array_multisort($keys, $sortType, $array);
}
sortBySubkey($arr, 'avgSearchVolume');

使用PHP 5.3,您可以像现在这样进行类似的函数调用。

function getSortVariable($sortType = SORT_ASC) {
    switch($sortType) {
        case SORT_ASC:
            return function ($a, $b) use ($subkey) { return strcmp($a[$subkey], $b[$subkey]); };
    }
}

function sortBySubkey(&$array, $subkey, $sortType = SORT_ASC) {
    $sortFunction = getSortVariable($sortType);
    usort($array, $sortFunction($subkey));
}

答案 2 :(得分:8)

您必须与usort()一起使用自定义回调功能。

function cmp($a, $b)
{
    if ($a['avgSearchVolume'] == $b['avgSearchVolume']) {
        return 0;
    }
    return ($a['avgSearchVolume'] > $b['avgSearchVolume']) ? -1 : 1;
}
usort($array, 'cmp');

答案 3 :(得分:2)

这可能会有所帮助:Sorting Arrays of Arrays

答案 4 :(得分:2)

function querySort ($first_Array,$second_Array) {
    return strcasecmp($first_Array['name'],$second_Array['name']);
}
echo '<pre>';
usort($main, 'querySort');

print_r($main);
die;

答案 5 :(得分:1)

这是另一种解决方案,您可以添加多个排序选项(请参阅代码的注释部分)

<?php

$arr=Array(
     Array("text" => "tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 7480000,"lastMonthSearchVolume" => 9140000),
     Array("text" => "personality tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 165000,"lastMonthSearchVolume"=>201000),
     Array("text" => "online tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 246000,"lastMonthSearchVolume" =>301000)
     );


$sort = array();
foreach($arr as $k=>$v) {
    $sort['avgSearchVolume'][$k] = $v['avgSearchVolume'];
    //$sort['text'][$k] = $v['text'];
}

array_multisort($sort['avgSearchVolume'], SORT_DESC, $arr);
//array_multisort($sort['avgSearchVolume'], SORT_DESC, $sort['text'], SORT_ASC,$arr);

echo "<pre>";
print_r($arr);

?>

参考:http://php.net/manual/en/function.array-multisort.php