note 118193 added to function.array-multisort
| From: | nospam at nospam dot com | Date: | Fri, 23 Oct 2015 19:34:21 +0000 |
| Subject: | note 118193 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204011@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2602:306:832a:9cc0:6006:bd21:ea40:84b
----
Manual Page -- http://php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/118193
Del: integrated -- https://master.php.net/note/delete/118193/integrated
Del: useless -- https://master.php.net/note/delete/118193/useless
Del: bad code -- https://master.php.net/note/delete/118193/bad+code
Del: spam -- https://master.php.net/note/delete/118193/spam
Del: non-english -- https://master.php.net/note/delete/118193/non-english
Del: in docs -- https://master.php.net/note/delete/118193/in+docs
Del: other reasons-- https://master.php.net/note/delete/118193
Reject -- https://master.php.net/note/reject/118193
Search -- https://master.php.net/manage/user-notes.php