简体   繁体   English

排序两个数组,以便第二个数组排序基于第一个

[英]Sorting two arrays so that the second array sorting is based on the first

I have two arrays. 我有两个数组。 First array contains user ids and the second array contains the number of matched answers against an excising data set. 第一个数组包含用户ID,第二个数组包含针对切除数据集的匹配答案数。 The size of these arrays will always be the same and currently the matched array value indexes correspond to those of user ids. 这些数组的大小将始终相同,并且当前匹配的数组值索引与用户ID对应。

useridArr = [1a,2a,3a,4a];
matched = [12,2,5,11];

So here user 1a has 12 matched answers, user 2a has 2 and so on. 因此,此处用户1a有12个匹配的答案,用户2a有2个,依此类推。 Now how can I sort the matched array in descending order and at the same time, sort the useridArr accordingly. 现在,我该如何按降序对匹配的数组进行排序,并同时对useridArr进行相应的排序。 Thanks 谢谢

useridArr = [1a,4a,3a,2a];
matched = [12,11,5,2];

You want array_multisort . 您需要array_multisort

array_multisort($matched, $userIdArr);

will sort both arrays as you require. 将根据需要对两个数组进行排序。

array_multisort($matched, SORT_DESC, $userIdArr);

will sort in descending order as required in your comment. 将按照您的评论要求以降序排列。 http://php.net/manual/en/function.array-multisort.php gives a lot more information on the capabilities of this function. http://php.net/manual/zh/function.array-multisort.php提供了有关此功能的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM