malikah Posted November 17, 2007 Share Posted November 17, 2007 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 More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 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 More sharing options...
malikah Posted November 17, 2007 Author Share Posted November 17, 2007 Yea - the print_r should have been echo - but still, what am I doing wrong? It looks like it should work.. right? Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393452 Share on other sites More sharing options...
malikah Posted November 17, 2007 Author Share Posted November 17, 2007 Here's the whole process: $qwe=array(""); while(list($zzz, $xxx) = mysqli_fetch_row($result)){ array_push($qwe, "$xxx "); } $result = array_unique($qwe); sort($result); foreach ($result as $ans){ echo $ans; } Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393457 Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 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 More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 You did set it as a string type. $qwe=array(""); Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393462 Share on other sites More sharing options...
rarebit Posted November 17, 2007 Share Posted November 17, 2007 No that was setting first as an empty string, but then... array_push($qwe, "$xxx "); Turns each element into a string with a space on end! Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393463 Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 Yup that was part of it. Try this: while(list($zzz, $xxx) = mysqli_fetch_row($result)){ $qwe[] =$xxx; } And also use code tag. Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393465 Share on other sites More sharing options...
malikah Posted November 17, 2007 Author Share Posted November 17, 2007 - Thanks guys - it works now: $qwe=array(); while(list($zzz, $xxx) = mysqli_fetch_row($result)){ array_push($qwe, "$xxx"); } $result = array_unique($qwe); sort($result); foreach ($result as $ans){ echo "$ans "; } Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393466 Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 Great. Don't forget to click "Topic Solved" Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393468 Share on other sites More sharing options...
malikah Posted November 17, 2007 Author Share Posted November 17, 2007 wsantos - nice n short idea - thank you Link to comment https://forums.phpfreaks.com/topic/77731-solved-sortxxx/#findComment-393469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.