note 63516 added to function.array-multisort
| From: | scott-evolove-net-workitout at osu1 dot php dot net | Date: | Thu, 23 Mar 2006 03:51:22 +0000 |
| Subject: | note 63516 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106416@lists.php.net to get a copy of this message | ||
A very simple way to sort an array of associative arrays by some value is to use usort.
I needed to sort an array of 20 data structures by their 'distance' value:
Array
(
[0] => Array
(
[blahblah] => blahblah
[distance] => 6
)
[1] => Array
(
you get the idea....
Here's the code:
--------------------
usort($results, "distributor_compare");
/**
* usort callback
*/
function distributor_compare($a, $b) {
$adist = intval($a['distance']);
$bdist = intval($b['distance']);
if ($adist == $bdist) {
return 0;
}
return ($adist < $bdist) ? -1 : 1;
}
--------------------
----
Server IP: 203.220.0.234
Probable Submitter: 203.206.235.237
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/note/edit/63516
Del: integrated -- http://master.php.net/note/delete/63516/integrated
Del: useless -- http://master.php.net/note/delete/63516/useless
Del: bad code -- http://master.php.net/note/delete/63516/bad+code
Del: spam -- http://master.php.net/note/delete/63516/spam
Del: non-english -- http://master.php.net/note/delete/63516/non-english
Del: in docs -- http://master.php.net/note/delete/63516/in+docs
Del: other reasons-- http://master.php.net/note/delete/63516
Reject -- http://master.php.net/note/reject/63516
Search -- http://master.php.net/manage/user-notes.php