加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP多维数组(usort)

发布时间:2020-12-13 17:18:17 所属栏目:PHP教程 来源:网络整理
导读:我有一个像这样的关联数组 Array( ["News 1"] = Array ( [text] = tests [language] = [advertiserCompetitionScale] = 5 [avgSearchVolume] = 7480000 [lastMonthSearchVolume] = 9140000 ) ["News 2"] = Array ( [text] = personality tests [language] =
我有一个像这样的关联数组

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

是否有任何解决方案使列保持列名称?

谢谢

解决方法

使用功能 uasort代替 usort,它保留了索引关联.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读