note 47936 added to function.uksort

From: Date: Mon, 06 Dec 2004 12:27:45 +0000
Subject: note 47936 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-81438@lists.php.net to get a copy of this message
Here is a small and very fast object to handle sorting of multidimentional arrays by a key. <?php /** * Handles multidimentional array sorting by a key (not recursive) * * @author Oliwier Ptak <aleczapka at gmx dot net> */ class array_sorter { var $skey = false; var $sarray = false; var $sasc = true; /** * Constructor * * @access public * @param mixed $array array to sort * @param string $key array key to sort by * @param boolean $asc sort order (ascending or descending) */ function array_sorter(&$array, $key, $asc=true) { $this->sarray = $array; $this->skey = $key; $this->sasc = $asc; } /** * Sort method * * @access public * @param boolean $remap if true reindex the array to rewrite indexes */ function sortit($remap=true) { $array = &$this->sarray; uksort($array, array($this, "_as_cmp")); if ($remap) { $tmp = array(); while (list($id, $data) = each($array)) $tmp[] = $data; return $tmp; } return $array; } /** * Custom sort function * * @access private * @param mixed $a an array entry * @param mixed $b an array entry */ function _as_cmp($a, $b) { //since uksort will pass here only indexes get real values from our array if (!is_array($a) && !is_array($b)) { $a = $this->sarray[$a][$this->skey]; $b = $this->sarray[$b][$this->skey]; } //if string - use string comparision if (!ctype_digit($a) && !ctype_digit($b)) { if ($this->sasc) return strcasecmp($a, $b); else return strcasecmp($b, $a); } else { if (intval($a) == intval($b)) return 0; if ($this->sasc) return (intval($a) > intval($b)) ? -1 : 1; else return (intval($a) > intval($b)) ? 1 : -1; } } }//end of class ?> Sample $input_array: Array ( [0] => Array ( [id] => 961 [uid] => 29 [gid] => 12 [parent_id] => 147 [created] => 20041206105350 [modified] => 20041206110702 ) [1] => Array ( [id] => 41 [uid] => 29 [gid] => 12 [parent_id] => 153 [created] => 20041025154009 [modified] => 20041206105532 ) [2] => Array ( [id] => 703 [uid] => 29 [gid] => 12 [parent_id] => 419 [created] => 20041025154132 [modified] => 20041027150259 ) Example of usage: <?php function multi_sort(&$array, $key, $asc=true) { $sorter = new array_sorter($array, $key, $asc); return $sorter->sortit(); } //sort by parent_id in descending order $my_array = multi_sort($input_array, "parent_id", false); ?> The result array will be: Array ( [0] => Array ( [id] => 703 [uid] => 29 [gid] => 12 [parent_id] => 419 [created] => 20041025154132 [modified] => 20041027150259 ) [1] => Array ( [id] => 41 [uid] => 29 [gid] => 12 [parent_id] => 153 [created] => 20041025154009 [modified] => 20041206105532 ) [2] => Array ( [id] => 961 [uid] => 29 [gid] => 12 [parent_id] => 147 [created] => 20041206105350 [modified] => 20041206110702 ) ---- Manual Page -- http://www.php.net/manual/en/function.uksort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47936 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47936&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47936&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81438) next »