Jump to content

[SOLVED] sort($xxx);


malikah

Recommended Posts

Hi, one of my arrays isn't being sorted the way I would like. I'm pulling a bunch of numerical results from a mysql database, then using array_unique(), then sorting them:

 

The results from the mysql database are:

 

128, 128, 128, 128, 128, 128, 128, 128, 128, 143, 143, 143, 143, 143, 143, 143, 143, 143, 207, 207, 207, 207, 207, 207, 207, 207, 207, 21, 21, 238, 238, 238, 238, 238, 238, 238, 238, 238, 42, 42, 42, 534, 534, 534, 534, 534, 534, 534, 71, 71, 71, 71, 71, 71, 71, 71, 71, 806, 806, 806, 806, 806, 806, 806, 806, 806, 863, 863, 863, 863, 863, 863, 880, 880, 880, 880, 880, 880, 880, 880, 880, 9, 9, 9, 9, 9, 9, 9, 9, 9, 985, 985, 985, 985, 985, 985, 985, 985, 985,

 

Then I use PHP to unique and sort them:

 

$result = array_unique($qwe);

sort($result);

foreach ($result as $ans){

print_r($ans);

}

 

The output is: 128 143 207 21 238 42 534 71 806 863 880 9 985.

 

As you can see, there's something not quite as expected. - Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/77731-solved-sortxxx/
Share on other sites

Why are you assuming it is an associative array (well if it's from mysql), here's what I tried:

$m = array(128, 128, 128, 128, 128, 128, 128, 128, 128, 143, 143, 143, 143, 143, 143, 143, 143, 143, 207, 207, 207, 207, 207, 207, 207, 207, 207, 21, 21, 238, 238, 238, 238, 238, 238, 238, 238, 238, 42, 42, 42, 534, 534, 534, 534, 534, 534, 534, 71, 71, 71, 71, 71, 71, 71, 71, 71, 806, 806, 806, 806, 806, 806, 806, 806, 806, 863, 863, 863, 863, 863, 863, 880, 880, 880, 880, 880, 880, 880, 880, 880, 9, 9, 9, 9, 9, 9, 9, 9, 9, 985, 985, 985, 985, 985, 985, 985, 985, 985);

$result = array_unique($m);
sort($result);
print_r($result);

 

And this was the result:

Array ( [0] => 9 [1] => 21 [2] => 42 [3] => 71 [4] => 128 [5] => 143 [6] => 207 [7] => 238 [8] => 534 [9] => 806 [10] => 863 [11] => 880 [12] => 985 ) 

Link to comment
https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393450
Share on other sites

Correct me if I'm wrong...you get your data from a mysql_query statement right? It seemed to me along the way you have implicitly converted the data to a string type hence the result of the sort is based on a string.  Check your code prior to what you have posted? Perhaps on your mysql_fetch loop.

Link to comment
https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393459
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.