4

我有一个像这样的关联数组

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 通过删除“新闻 1”、“新闻 2”等破坏了我的关联数组。并将其替换为 0、1、2 ......

有什么解决方案可以让 sort 保留列名吗?

谢谢

4

2 回答 2

4

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

于 2012-02-28T13:59:57.173 回答
1

请改用uasortusort不维护关联键,而uasort确实如此。

于 2012-02-28T14:03:30.200 回答