user3642365
user3642365

Reputation: 579

How to sort one array and use key order to sort a second array in PHP?

Right now I have two arrays - $totaltimeplayed and $gameslist where each value in $totaltimeplayed refers to time spent playing the corresponding game in $gameslist. I want to sort the time spent playing a game in $totaltimeplayed, and then accordingly sort the games in $gameslist.

My code so far:

arsort($totaltimeplayed);
$key_order = array_keys($totaltimeplayed);
$sort_games = array_combine($key_order, $gameslist);
ksort($sort_games);

When I use print_r(), it seems the sorting of $gameslist is arbitrary and in no particular order.

What is the problem?

Upvotes: 1

Views: 44

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

I love array_multisort:

array_multisort($totaltimeplayed, SORT_DESC, $gameslist);

Upvotes: 2

Related Questions