phparrayssorting

ksort()'s return value prints as 1 instead of the sorted array


I have the following array:

$arr = array('XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
                                    

Now, using a function, I generate the following array based on the previous one. When doing a print_r($new_arr) and echoing it in pre tags, this is the output:

Array
(
    [4] => l
    [3] => m
    [2] => s
    [5] => xl
    [1] => xs
)

However, I'm trying to achieve this:

Array
(
    [1] => xs
    [2] => s
    [3] => m
    [4] => l
    [5] => xl
)

Now, I did some searching on S.O and found about the ksort function. Further reading in the PHP Docs, shows that this is the one to be used but when I use ksort as follows and the echoing the output, I get the value 1 only, instead of an array of values as I expected to get in the previous paragraph above:

$sorted_arr = ksort($new_arr);

On echoing $sorted_arr in pre tags, this is the output (it's just the number one):

1

I'm not really sure what is wrong here.


Solution

  • ksort returns a boolean value not the sorted array. Output the $new_arr variable instead.