note 118194 added to function.array-multisort

From: Date: Fri, 23 Oct 2015 22:30:20 +0000
Subject: note 118194 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-204012@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! Preserves the original multiarray order if the sorting values are equal. <?php // sorts multiarray by a subarray value while preserving all keys, also preserves original order when the sorting values match 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["$a[$sortkey]"][$k] = $a; // temp ma with sort value, quotes convert key to string in case numeric float if ($sortorder == 2) { // descending krsort($temp); } else { // ascending ksort($temp); } $newma = array(); // blank output multiarray to add to foreach ($temp as $sma) $newma += $sma; // add sorted arrays to output array unset($ma, $sma, $temp); // release memory return $newma; } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 2602:306:832a:9cc0:cf7:8416:65d4:55b1 ---- Manual Page -- http://php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/118194 Del: integrated -- https://master.php.net/note/delete/118194/integrated Del: useless -- https://master.php.net/note/delete/118194/useless Del: bad code -- https://master.php.net/note/delete/118194/bad+code Del: spam -- https://master.php.net/note/delete/118194/spam Del: non-english -- https://master.php.net/note/delete/118194/non-english Del: in docs -- https://master.php.net/note/delete/118194/in+docs Del: other reasons-- https://master.php.net/note/delete/118194 Reject -- https://master.php.net/note/reject/118194 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204012) next »