Array sorting in php

drf1229

New Member
Messages
71
Reaction score
1
Points
0
I'm trying to sort two arrays in php. One is full of strings and another integers. What I want to do is sort the integers by numeric order and then sort the strings the exact same way as integers. The reason it needs to be so perfect us because the first array is a set of usernames, the second is a set of scores, so they correspond like this: $firstarray[1] is the name for $secondarray[1]. I'm trying to sort out the scores by numeric order. Any suggestions for how to do this? Any advice is greatly appreciated!
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Two options: use array_multisort or combine the arrays with array_combine and sort the resulting array using asort or arsort. You'll need to use the usernames as keys, since scores may not be unique.

PHP:
// option 1:
array_multisort($scores, $users);

// option 2:
$userscores = array_combine($users, $scores);
asort($userscores);
 
Top