note 45283 added to function.array-multisort
| From: | jon at PLEASENOSPAMMY dot webtrick dot com | Date: | Wed, 01 Sep 2004 01:12:17 +0000 |
| Subject: | note 45283 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75812@lists.php.net to get a copy of this message | ||
I had trouble understanding this function, and had trouble getting the array sorted by multiple
columns (like ORDER BY last_name, first_name). What I finally figured out is that each ORDER BY
parameter gets its own array. So below, $sortarr1 is a list of last names, and $sortarr2 is a list
of first names.
I hope this helps somebody!
<?
function sortMultiArray($marray, $column,$column2='') {
//use the existing array to build the sorting array(s)
foreach ($marray as $row) {
$sortarr1[] = $row[$column];
if ($column2)
$sortarr2[] = $row[$column2];
}
//$sortarr1 has all the last names,
//$sortarr2, has all the first names
// the first two arrays are used to sort the last array
array_multisort($sortarr1,$sortarr2, $marray);
return $marray;
}
//calling the function
sortMultiArray($anArray,'last_name','first_name');
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45283
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45283&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45283&report=yes
Search -- http://master.php.net/manage/user-notes.php