我有一个关联数组,如下所示
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通过删除“News1”、“News2”等来破坏我的关联数组。并将其替换为0,1,2...
有没有办法让sort保留列名?
谢谢
发布于 2012-02-28 21:59:57
使用函数uasort代替usort,该函数保留了索引关联。
发布于 2012-02-28 22:03:30
请改用uasort。usort不维护关联键,而uasort维护关联键。
https://stackoverflow.com/questions/9483215
复制相似问题