[英]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 ......
是否有任何解決方案使列保持列名稱?
謝謝
請改用uasort 。 usort
不維護關聯鍵,而uasort
則維護關聯鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.