jimiss
jimiss

Reputation: 119

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. The size of these arrays will always be the same and currently the matched array value indexes correspond to those of user ids.

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. Now how can I sort the matched array in descending order and at the same time, sort the useridArr accordingly. Thanks

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

Upvotes: 0

Views: 67

Answers (1)

Simon Brahan
Simon Brahan

Reputation: 2076

You want 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.

Upvotes: 3

Related Questions