note 75137 modified in function.sort by nlopess
| From: | nlopess@php.net | Date: | Tue, 15 May 2007 13:48:20 +0000 |
| Subject: | note 75137 modified in function.sort by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126610@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).
--was--
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/
http://php.net/manual/en/function.sort.php