note 75137 added to function.sort
| From: | ((Qube#php at Efnet)) | Date: | Mon, 14 May 2007 22:38:57 +0000 |
| Subject: | note 75137 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126588@lists.php.net to get a copy of this message | ||
How to use an anonymous array to sort any associative array by an arbitrary key (or nested key):
$order = -1; # -1 = Ascending. Use 1 for descending.
$sortby = "['key1']['subkey']";
$mysort = create_function('$a,$b', "\$a1=\$a$sortby;\$b1=\$b$sortby; if (\$a1==\$b1)
return 0; else return (\$a1<\$b1) ? $order : 0- $
order;");
uasort($assocarray, $mysort);
You can use this in a recursive function if necessary (which is why I developed it).
http://www.pgregg.com/projects/
----
Server IP: 194.145.210.3
Probable Submitter: 80.76.201.50
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/75137
Del: integrated -- https://master.php.net/note/delete/75137/integrated
Del: useless -- https://master.php.net/note/delete/75137/useless
Del: bad code -- https://master.php.net/note/delete/75137/bad+code
Del: spam -- https://master.php.net/note/delete/75137/spam
Del: non-english -- https://master.php.net/note/delete/75137/non-english
Del: in docs -- https://master.php.net/note/delete/75137/in+docs
Del: other reasons-- https://master.php.net/note/delete/75137
Reject -- https://master.php.net/note/reject/75137
Search -- https://master.php.net/manage/user-notes.php