Re: Sort Array not working
| From: | Fredrik Thunberg | Date: | Tue, 09 Jan 2007 18:47:45 +0000 |
| Subject: | Re: Sort Array not working | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-246810@lists.php.net to get a copy of this message | ||
Hi
sort returns a bool, the sorted array passed by reference.
So try:
$result = sort( $array );
//Now $array is sorted
print_r( $array );
/Fredrik Thunberg
Kevin Murphy skrev:
I'm having trouble sorting an array. When I do, it empties the array for some reason. Take the following code: $data = "bird,dog,cat,dog,,horse,bird,bird,bird,lizard";$array = explode(",",$data); // Create the array $array = array_diff($array, array("")); // Drop the empty elements $array = array_unique($array); // Drop the duplicate elements $array = array_merge($array); // Reset the array keysprint_r ($array); Up to here it works great. The resulting output is: Array ( [0] => bird [1] => dog [2] => cat [3] => horse [4] => lizard ) Then if I do this:$array = sort($array); // Sorts the arrayprint_r ($array); The output is "1". I've tried asort, rsort in the place of sort and get the same results. I've also tried putting the sort in between the first 4 steps and that doesn't work either. Obviously I'm doing something wrong here. I'm using PHP 4.3.4. Any suggestions? --Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326