This question already has an answer here:
I am stuck with this, and I can't figure out the solution.
I have 2 arrays:
$user_names = array();
$user_ids = array();
Which look like this
Array
(
[0] => John
[1] => Peter
[2] => Anna
)
Array
(
[0] => 67
[1] => 68
[2] => 73
)
I wanted to sort this array by $user_names with sort($user_names) but when I sort the user names then my user_ids are not matching up..
So if I wanted to print "User: John with id: X" after sorting user_names my ids don't match. I am dead stuck with this...
</div>
You can combine id and names to one array and sort that array with asort to preserve the keys.
$user_names = ["John", "Peter", "Anna"];
$user_ids = [67,68,73];
$users = array_combine($user_ids, $user_names);
asort($users);
Var_dump($users);
Now key is the userid and value is the username.
This can easily be echoed with a foreach.