note 57161 added to function.array-multisort
| From: | RWC at osu1 dot php dot net | Date: | Sun, 25 Sep 2005 20:45:53 +0000 |
| Subject: | note 57161 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95769@lists.php.net to get a copy of this message | ||
This is the simpler version of the function by AlberT.
A lot of times you have got an array like this:
$test[0]['name']='Peter';
$test[0]['points']=1;
$test[1]['name']='Mike';
$test[1]['points']=5;
$test[2]['name']='John';
$test[2]['points']=2;
You just want to sort on the index in the second dimension, ie. on points in the above example.
You can use the function below and call it like this:
$test = multi_sort($test, $key = 'points');
function multi_sort($array, $akey)
{
function compare($a, $b)
{
global $key;
return strcmp($a[$key], $b[$key]);
}
usort($array, "compare");
return $array;
}
Note: to be able to use $key in the compare function, it can not simply be passed as a parameter. It
has to be declared global and set somewhere outside of compare().
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57161
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57161&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57161&report=yes
Search -- http://master.php.net/manage/user-notes.php