RE: [PHP] Sort an array by it's values
| From: | Jeff McKeon | Date: | Mon, 26 Apr 2004 14:13:59 +0000 |
| Subject: | RE: [PHP] Sort an array by it's values | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-184648@lists.php.net to get a copy of this message | ||
> asort() /does/ sort by element value, it just maintains index
> association. Plain old sort() does not.
Ok, how would I then print out the array in the newly sorted order?
$names[]= alice;
$names[]= candy;
$names[]= dave;
$names[]= bob;
While(list($key,$element)=each($names))
{
echo"$key, $element<BR>";
}
Returns:
1,alice
2,candy
3,dave
4,bob
Asort($names);
While(list($key,$element)=each($names))
{
echo"$key, $element<BR>";
}
Also returns:
1,alice
2,candy
3,dave
4,bob
What am I missing here?
Jeff
> -----Original Message-----
> From: Michal Migurski [mailto:mike@teczno.com]
> Sent: Monday, April 26, 2004 10:09 AM
> To: Jeff McKeon
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Sort an array by it's values
>
>
> > Is there a command similar to asort() that will sort the
> elements of
> > an array not by their key values but by the element values?
>
> asort() /does/ sort by element value, it just maintains index
> association. Plain old sort() does not.
>
> If you actually want to sort by key value, you want ksort().
>
> There's a complete list of all the sort functions at:
> http://php.net/manual/en/ref.array.php
>
> ---------------------------------------------------------------------
> michal migurski- contact info and pgp key:
> sf/ca http://mike.teczno.com/contact.html
>
>