簡體   English   中英

PHP多維數組(usort)

[英]PHP multidimensional array (usort)

我有一個像這樣的關聯數組

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 ......

是否有任何解決方案使列保持列名稱?

謝謝

代替usort ,使用函數uasort ,它保留索引關聯。

請改用uasort usort不維護關聯鍵,而uasort則維護關聯鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM