note 76330 added to function.sort

From: Date: Tue, 10 Jul 2007 22:03:44 +0000
Subject: note 76330 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-128368@lists.php.net to get a copy of this message
The sort2d I posted before did nothing by default--that'll teach me to copy-and-paste without thinking. Its sort function has to be associative. I've changed the default to asort. natcasesort does actually work, though. // $sort used as variable function--can be natcasesort, for example // WARNING: $sort must be associative function sort2d( &$arrIn, $index = null, $sort = 'asort') { // pseudo-secure--never allow user input into $sort if (strpos($sort, 'sort') === false) {$sort = 'asort';} $arrTemp = Array(); $arrOut = Array(); foreach ( $arrIn as $key=>$value ) { $arrTemp[$key] = is_null($index) ? reset($value) : $value[$index]; } $sort($arrTemp); foreach ( $arrTemp as $key=>$value ) { $arrOut[$key] = $arrIn[$key]; } $arrIn = $arrOut; } Also, uasort is probably actually the better solution for most 2d sorting, unless you're sorting by a dozen different indexes. ---- Server IP: 69.147.83.197 Probable Submitter: 64.105.153.106 ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- https://master.php.net/note/edit/76330 Del: integrated -- https://master.php.net/note/delete/76330/integrated Del: useless -- https://master.php.net/note/delete/76330/useless Del: bad code -- https://master.php.net/note/delete/76330/bad+code Del: spam -- https://master.php.net/note/delete/76330/spam Del: non-english -- https://master.php.net/note/delete/76330/non-english Del: in docs -- https://master.php.net/note/delete/76330/in+docs Del: other reasons-- https://master.php.net/note/delete/76330 Reject -- https://master.php.net/note/reject/76330 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128368) next »