note 27583 added to function.usort
| From: | mharrodine at rack1 dot php dot net | Date: | Wed, 11 Dec 2002 22:46:33 +0000 |
| Subject: | note 27583 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40772@lists.php.net to get a copy of this message | ||
Thankyou to franky at iname dot com for his solution to my problem although i'd like to clarify
something because the use for this isn't entirely obvious at first glance. I like to define my
arrays to look like tables in a database or spreadsheet as follows (it looks tidy is the only
reason!):
Row(s) Column(s) ------------->
| $array[0] = array("1", "2","3");
| $array[1] = array("1", "2","3");
\/ $array[2] = array("1", "2","3");
This "array of arrays" seems to behave differently to normal associative or
multi-dimension arrays when sorting but using franky's routine....
function cmp ($a, $b)
{
global $w_o;
if ($a[$w_o] == $b[$w_o]) return 0;
return ($a[$w_o] < $b[$w_o]) ? -1 : 1;
}
...you simply specify the column you want to sort by defining $w_o and call
"usort($my_array,"cmp");". This might seem obvious to some people but
wasn't to me and I hope this helps others in the same situation. Thanks....
--
http://www.php.net/manual/en/function.usort.php
http://master.php.net/manage/user-notes.php?action=edit+27583
http://master.php.net/manage/user-notes.php?action=delete+27583
http://master.php.net/manage/user-notes.php?action=reject+27583