phparrayssorting

sort() function is not returning the sorted array


I am trying to Sort the following Array but for some odd reason it doesn't seem to work

$sizearray = Array ( 
    [0] => 39 
    [1] => 40 
    [2] => 41 
    [3] => 42 
    [4] => 43 
    [5] => 44 
    [6] => 45 
    [7] => 39 
    [8] => 40 
    [9] => 41 
    [10] => 42 
    [11] => 43 
    [12] => 44 
    [13] => 45 
    [14] => 39 
    [15] => >40 
    [16] => 41 
    [17] => 42 
    [18] => 43 
    [19] => 44 
    [20] => 45 
);

$sizearray = array_values(sort(array_unique($sizearray)));

And the following warnings shows up:

>Warning: array_values() [function.array-values]: The argument should be an array in 
>/home/starlet/public_html/productlist.php on line 349

Note: If i remove sort() function, the array_values() function runs fine.


Solution

  • That's because sort is in-place and returns a boolean.

    From the docs:

    bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
    

    You'll probably need to do something like this:

    $sizearray = array_unique($sizearray);
    sort($sizearray);
    $sizearray = array_values($sizearray);