note 45283 deleted from function.array-multisort by aidan
| From: | aidan@php.net | Date: | Wed, 01 Sep 2004 06:35:44 +0000 |
| Subject: | note 45283 deleted from function.array-multisort by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75817@lists.php.net to get a copy of this message | ||
Note Submitter: jon at PLEASENOSPAMMY dot webtrick dot com
----
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');
?>