[英]Two arrays, one array to list items, second to sort the first one, php code how to?
So I got function that prints out array score which concert clinics: 所以我得到了可以打印出音乐会诊所的数组分数的函数:
array_id clinic_id score_amount
0 1 5
1 2 1
2 3 5
3 5 3
4 8 2
And i got another function that prints out array clinics id like this: This array is for listing all clinics on page, 而且我还有另一个功能可以像这样打印阵列诊所ID:该阵列用于列出页面上的所有诊所,
array_id clinic_id
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
Now my idea is to make array of clinic_id sorted accordinally to score_amount from first array. 现在,我的想法是使climate_id数组按照第一个数组的score_amount进行排序。 And theen if there is no score_amount for particular clinic_id, just go with order from second array normally.
然后,如果没有针对特定临床编号的score_amount,则通常按照第二个数组的顺序进行。
So results needs to be sorted like: 因此,结果需要按以下方式排序:
array_id clinic_id
0 1
1 3
2 5
3 8
4 2
5 4
6 6
7 7
8 9
9 10
您可以使用关联数组(clinic_id => score_amount)并使用asort 。
use a custom comparator, 使用自定义比较器
function cmp($a, $b)
{
if($a == $b) return 0;
\\get score returns score or -1 if no score exists
if (getscore($a) == getscore($b) {
return (index($a) < index($b)) ? -1 : 1; \\index is index of the number in second array
}
return (getscore($a) < getscore($b)) ? -1 : 1;
}
usort($a, "cmp");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.