views:

44

answers:

2

Hello

I'm trying to sort a simple array which only contains a few decimal numbers.

e.g:

( [0] => 0.05 [1] => 0.076 [2] => 0.092 )

using this:

$stuff = sort ($comparison);

However when I use the php sort, asort ect functions, instead of getting a sorted array, I get the number 1. Very confusing! Any help?

+5  A: 

Check out what the manual page on sort() has to say:

Return Values

Returns TRUE on success or FALSE on failure.

sort() will sort the array you pass to it directly.

The fact that some array functions do it this way, and others the other way, is admittedly confusing though. The inconsistency of the core library is one of the key points of many people criticizing PHP.

Pekka
It doesn't return the sorted array?
YsoL8
@Yso nope. ----
Pekka
No. The passed array is now sorted.
Amadan
OK I'm sorted out now. Simply removed the $variable = bit. (very bad pun there).
YsoL8
+1  A: 

The sort function returns true or false based on success or failure, the array which is passed as the argument is sorted and can be referenced using the original variable $array in this case.

$array = array(0.092, 0.76, 0.05);
print_r($array);
    //( [0] => 0.092 [1] => 0.76 [2] => 0.05 ) 
sort($array);
print_r($array);
     //( [0] => 0.05 [1] => 0.092 [2] => 0.76 ) 
jkilbride