简体   繁体   English

两个数组,一个数组列出项目,第二个对第一个进行排序,php代码如何?

[英]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.

相关问题 PHP将两个数组合并为一个表。 第一阵列垂直,第二水平 - PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal 将两个数组合并到一个表中。 第一列垂直,第二列水平 - Two arrays into one table. First array vertical, second horizontal 合并两个数组,用第二个数组覆盖第一个数组 - Merging two arrays, overwriting first array with second one 如何通过仅接管与第一个具有相同键的第二个数组中的值来合并两个 arrays? - How to merge two arrays by taking over only values from the second array that has the same keys as the first one? 两个php数组 - 使用另一个数组的值排序一个数组 - two php arrays - sort one array with the value order of another 如何按第一个顺序对两个或更多数组进行排序? - How to sort two OR MORE arrays by the order of first one? PHP 将两个数组合并到一个表中。 第一个阵列垂直和水平 - PHP Two Arrays Into One Table. First Array Vertical & Horizontal 一个数组中的两个数组PHP - Two arrays in one array PHP 首先根据值合并两个数组,然后根据key-php合并第二个数组 - Merge two arrays first one based on value and second one based on key - php PHP 代码 - 将二维 arrays 转换为一维数组 $vector - 函数 toVector 和 toArray - 怎么做? - PHP code - converting two dimensional arrays to one dimensional array $ vector - functions toVector and toArray - how to do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM