Skip to content
Advertisement

Sort array and keep values of keys

I have an array that looks like this:

JavaScript

and I would like to sort the teams by the number of points each team has. I have tried this:

JavaScript

But that approach overrides the keys containing the teamnames and returns:

JavaScript

Is there any way to sort the array without overwriting the teamnames as the array keys?

Advertisement

Answer

Use the uasort function, that should keep the key => value associations intact.

(side note: you can do return $a['points'] - $b['points'] instead of the ifs)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement