note 118193 deleted from function.array-multisort by cmb
| From: | cmb@php.net | Date: | Wed, 16 Dec 2015 15:49:35 +0000 |
| Subject: | note 118193 deleted from function.array-multisort by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-204410@lists.php.net to get a copy of this message | ||
Note Submitter: nospam at nospam dot com
----
Super easy and simple way to sort a keyed multiarray while maintaining all associative keys
including numeric. This is awesome!
<?php
// returns sorted multiarray by its subarray's sortkey in given sortorder while keeping all
keys
function maSort($ma = '', $sortkey = '', $sortorder = 1) { // sortorder: 1=asc,
2=desc
if ($ma && is_array($ma) && $sortkey) { // confirm inputs
foreach ($ma as $k=>$a) $temp[$k] = $a[$sortkey]; // save temp array to be sorted
if ($sortorder == 2) { // descending
arsort($temp);
} else {
asort($temp); // ascending
}
$newma = array_replace($temp, $ma); // combine to keep order of sorted array but with original
$ma info
unset($ma, $temp); // release memory
return $newma;
}
}
?>