phparrayssorting

sort() prints 1 instead of the sorted array


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

array(
   0.05,
   0.076,
   0.092
)

using this:

$stuff = sort($comparison);

However when I use the php sort(), asort() etc. functions, instead of getting a sorted array, I get the number 1.


Solution

  • 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.