phparrayssortingnatural-sort

How to naturally sort a flat array with arsort()?


I have an array:

Array (
 [0] => 99a1
 [8] => 75a4
 [7] => 75a3
 [6] => 75a2
 [5] => 75a1
 [9] => 150a5
 [2] => 150a1
 [4] => 150a1
 [1] => 150a1
 [3] => 131a1
)

I want to sort the array so that the first numbers would be 150a1, however when I run `arsort($_SESSION["highscore"]);, it stays like the array above.

EDIT:

array_push($_SESSION["highscore"], $_SESSION['score'] . 'a' . $_SESSION['scoreNumber']);
arsort($_SESSION["highscore"]);
print_r($_SESSION["highscore"]);

for ($i = 0; $i <= count($_SESSION["highscore"]) - 1; $i++) { 
    if ($_SESSION["highscore"][$i] == $_SESSION['score'] . 'a' . $_SESSION['scoreNumber']) {
        $scoreInArray = $i;
    }
}

Solution

  • Using SORT_NATURAL (similar to natsort()) will sort the string according to numerical as well as character order...

    arsort($_SESSION["highscore"], SORT_NATURAL);