note 109840 deleted from function.sort by danbrown

From: Date: Fri, 31 Aug 2012 15:41:39 +0000
Subject: note 109840 deleted from function.sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190319@lists.php.net to get a copy of this message
Note Submitter: ryanericw at gmail dot com ---- I saw another user on here post a snippet for sorting multi-dimensional associative arrays by using a specified index WITHOUT losing the relationships. I took part of that code and added more to make a fully functional .... function. function mArray_sort(&$input_array,$key,$order='ASC') { function sorter($key,$order) { if($order == "ASC") { return function ($a, $b) use ($key) { return strcasecmp($a[$key], $b[$key]); }; } elseif($order == "DESC") { return function ($b, $a) use ($key) { return strcasecmp($a[$key], $b[$key]); }; } } usort($input_array, sorter($key,strtoupper($order))); } Hope it helps someone in the future.

« previous php.notes (#190319) next »