phparrayssortingksort

PHP sort associative array by numeric key in asc order


I have a problem with ksort, it is print_r'ing 1 instead of the array.

Here is my array:

Array(

[1] => Array
    (
        [time] => 08:30 am
        [time_id] => 48451
    )

[0] => Array
    (
        [time] => 09:00 am
        [time_id] => 48452
    )

[2] => Array
    (
        [time] => 09:30 am
        [time_id] => 48453
    )

)

And ksort($array) is vardumping bool(true). Why is it not sorting my array appropriately?

echo '<pre>';
print_r($array);
$array = ksort($array);
var_dump($array);

Solution

  • Why is it not sorting my array appropriately?

    ksort() works on the array directly and returns a bool - returning true on success and false otherwise.

    print_r() outputs 1 (i.e. true) because ksort() successfully sorted the array.

    Don't reassign $array.