PHP多维数组(usort)

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通过移除“News 1”,“News 2”等来破坏了我的关联数组,并将其替换为0、1、2...是否有任何解决方案可以让排序保留列名?谢谢。
2个回答

4

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


1
使用uasort代替usortusort不保持关联键,而uasort保持关联键。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接