在 PHP 中对关联数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting an associative array in PHP
提问by Click Upvote
I have an array in this format:
我有一个这种格式的数组:
Array
(
[0] => Array
(
[text] => tests
[language] =>
[advertiserCompetitionScale] => 5
[avgSearchVolume] => 7480000
[lastMonthSearchVolume] => 9140000
)
[1] => Array
(
[text] => personality tests
[language] =>
[advertiserCompetitionScale] => 5
[avgSearchVolume] => 165000
[lastMonthSearchVolume] => 201000
)
[2] => Array
(
[text] => online tests
[language] =>
[advertiserCompetitionScale] => 5
[avgSearchVolume] => 246000
[lastMonthSearchVolume] => 301000
)
)
How can I sort an array in that format, in the descending order of the avgSearchVolumefield? Is there a built in function for this?
如何按avgSearchVolume字段的降序对这种格式的数组进行排序?是否有内置功能?
回答by Paul Dixon
回答by OIS
Until PHP 5.3 this is the best function for sorting based on subkeys without making a new function for each key.
在 PHP 5.3 之前,这是基于子键进行排序的最佳功能,而无需为每个键创建新功能。
function sortBySubkey(&$array, $subkey, $sortType = SORT_ASC) {
foreach ($array as $subarray) {
$keys[] = $subarray[$subkey];
}
array_multisort($keys, $sortType, $array);
}
sortBySubkey($arr, 'avgSearchVolume');
With PHP 5.3 you can make something like this, same function call as now.
使用 PHP 5.3,您可以进行与现在相同的函数调用。
function getSortVariable($sortType = SORT_ASC) {
switch($sortType) {
case SORT_ASC:
return function ($a, $b) use ($subkey) { return strcmp($a[$subkey], $b[$subkey]); };
}
}
function sortBySubkey(&$array, $subkey, $sortType = SORT_ASC) {
$sortFunction = getSortVariable($sortType);
usort($array, $sortFunction($subkey));
}
回答by Stefan Gehrig
回答by dev4092
function querySort ($first_Array,$second_Array) {
return strcasecmp($first_Array['name'],$second_Array['name']);
}
echo '<pre>';
usort($main, 'querySort');
print_r($main);
die;
回答by Prasanth Bendra
Here is another solution, You can add multiple options for sorting(See the commented section of the code)
这是另一种解决方案,您可以添加多个排序选项(请参阅代码的注释部分)
<?php
$arr=Array(
Array("text" => "tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 7480000,"lastMonthSearchVolume" => 9140000),
Array("text" => "personality tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 165000,"lastMonthSearchVolume"=>201000),
Array("text" => "online tests","language" =>"","advertiserCompetitionScale" => 5,"avgSearchVolume" => 246000,"lastMonthSearchVolume" =>301000)
);
$sort = array();
foreach($arr as $k=>$v) {
$sort['avgSearchVolume'][$k] = $v['avgSearchVolume'];
//$sort['text'][$k] = $v['text'];
}
array_multisort($sort['avgSearchVolume'], SORT_DESC, $arr);
//array_multisort($sort['avgSearchVolume'], SORT_DESC, $sort['text'], SORT_ASC,$arr);
echo "<pre>";
print_r($arr);
?>

