note 27371 added to function.sort
| From: | mbevan at rack1 dot php dot net | Date: | Tue, 03 Dec 2002 19:21:18 +0000 |
| Subject: | note 27371 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40306@lists.php.net to get a copy of this message | ||
I've noticed that if you try to sort an assoc. array, it strips the names of the elements, as
can be seen by the following code and it's output:
<?php
$fruitcolors = array ("lemon"=>"yellow",
"orange"=>"orange",
"banana"=>"yellow",
"apple"=>"red");
sort ($fruitcolors);
reset ($fruitcolors);
print_r($fruitcolors);
}
?>
Output:
Array
(
[0] => orange
[1] => red
[2] => yellow
[3] => yellow
)
Which is useless if the name of each element is important (as it is in the above example). How to
sort the above array without loosing the keys? There has to be a simple way of doing this that is
elusive to me.
--
http://www.php.net/manual/en/function.sort.php
http://master.php.net/manage/user-notes.php?action=edit+27371
http://master.php.net/manage/user-notes.php?action=delete+27371
http://master.php.net/manage/user-notes.php?action=reject+27371