我有一个这样的联合数组

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

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

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

)

我设法按我想要的列排序(例如lastmonthsearchvolume)
// compare function
function cmpi($a, $b)
{

     return strcmp($a["lastMonthSearchVolume"], $b["lastMonthSearchVolume"]);
}

// do the array sorting
usort($latest_array, 'cmpi');

问题是,当我转储数组以查看结果时,usort通过删除“news 1”、“news 2”等来断开关联数组。用0,1,2替换它…
有什么办法可以使sort保留列名吗?
谢谢

最佳答案

代替usort,使用函数uasort,它保留索引关联。

07-27 22:38