[英]Sorting a multidimensional array by 2 different values in PHP
I have an array(below). 我有一个数组(下)。 I want to sort it by 2 different values, first by the value in "override" which will be an integer 1-9.
我想按2个不同的值对其进行排序,首先按“ Override”中的值进行排序,该值将是1-9的整数。 Then if 0 or null, I want the array to sort by "total_rank".
然后,如果0或null,我希望数组按“ total_rank”排序。 So if override has 3 different values 213, and then total rank being 1.4, 1.6, 1.2, The array would be re-organized to first the row with override - 1, 2, 3. The next row would be the row with total_rank being 1.2, then 1.4, 1.6.
因此,如果override具有3个不同的值213,然后总排名为1.4、1.6、1.2,则该数组将重新组织为override-1,2,3的第一行。下一行将是total_rank为1.2,然后是1.4、1.6。
Sorry if I'm not explaining this as clear as I would like to. 抱歉,如果我没有按照我的意愿解释清楚。 I tried using arsort(), but couldnt get it to do what I want (I'm new to PHP).
我尝试使用arsort(),但无法使其执行我想要的操作(我是PHP的新手)。
Any help would be appreciated, A sample of a row of the multidimensional array is below: 任何帮助将不胜感激,多维数组的一行示例如下:
array(16) {
["id"]=>
string(1) "3"
["title"]=>
string(5) "test2"
["description"]=>
string(5) "test2"
["requester"]=>
string(1) "1"
["project_id"]=>
string(1) "2"
["client_ranking"]=>
string(1) "5"
["tech_ranking"]=>
string(1) "5"
["time_ranking"]=>
string(1) "5"
["pm_ranking"]=>
string(1) "5"
["total_rank"]=>
string(3) "1.8"
["datecreated"]=>
string(19) "2012-01-05 11:58:13"
["dateclosed"]=>
string(19) "2012-01-05 11:58:13"
["ispending"]=>
string(1) "1"
["isclosed"]=>
string(1) "0"
["override"]=>
string(1) "5"
["developer"]=>
string(1) "1"
If I understood you correctly, you could try using usort
: 如果我对您的理解正确,则可以尝试使用
usort
:
function cmp($a, $b)
{
// if 'override' is same we compare 'total_rank'
if ($a['override'] == $b['override']) {
if ($a['total_rank'] == $b['total_rank'])
return 0;
return ($a['total_rank'] < $b['total_rank']) ? -1 : 1;
}
// else compare 'override'
return ($a['override'] < $b['override']) ? -1 : 1;
}
usort($array, "cmp");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.