本文介绍了PHP多维数组(usort)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an associative array like this

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为例)

I managed to sort it by the column i want (LastMonthSearchVolume for example)

// 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代替它打破了我的关联数组...

The problem is when i dump the array to see the result the usort broke my associative array by removing "News 1", "News 2" etc.. and replacing it by 0,1,2...

有没有什么解决方案,以保持排序的列名?

Is there any solution to make sort keep the column name ?

感谢

推荐答案

在地方,使用功能 uasort ,其中preserves指数关联。

In place of usort, use the function uasort, which preserves index association.

这篇关于PHP多维数组(usort)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!