note 71152 added to function.uksort

From: Date: Tue, 14 Nov 2006 08:13:41 +0000
Subject: note 71152 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-119707@lists.php.net to get a copy of this message
Since, so many people reused array_sorter class, I decided to put here some updated version with fixes. <?php // // $Id: array_sorter.inc.php 82 2005-11-17 17:14:39Z aleczapka $ // /** * Handles multidimentional array sorting by a key (not recursive). * * @author Oliwier Ptak <aleczapka@gmail.com> */ class array_sorter { var $skey = false; var $sarray = false; var $sasc = true; var $sas_object = false; /** * 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, $as_object=false) // {{{ { $this->sarray = $array; $this->skey = $key; $this->sasc = $asc; $this->sas_object = $as_object; } // }}} function debug() // {{{ { echo "skey: ".$this->skey."<br>"; echo "sasc: ".$this->sasc."<br>"; } // }}} /** * Sort method * * @access public * @param boolean $remap if true reindex the array to reset indexes */ function sortit($remap=true) // {{{ { if (!is_array($this->sarray) || !array_key_exists($this->skey, @$this->sarray[0])) return $this->sarray; //$this->debug(); uksort($this->sarray, array($this, "_as_cmp")); if ($remap) { $tmp = array(); while (list($id, $data) = each($this->sarray)) $tmp[] = $data; return $tmp; } return $this->sarray; } // }}} /** * 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)) { //sort objects if ($this->sas_object) { $obj_a = $this->sarray[$a]; $obj_b = $this->sarray[$b]; $str = "\$a = \$obj_a->$this->skey;"; $str .= "\$b = \$obj_b->$this->skey;"; eval($str); } else { $a = $this->sarray[$a][$this->skey]; $b = $this->sarray[$b][$this->skey]; } } //if string - use string comparision if (!ctype_digit($a) && !ctype_digit($b) && !is_int($a) && !is_int($b)) { if ($this->sasc) return strcasecmp($a, $b); else return strcasecmp($b, $a); } else { if ($a == $b) return 0; if ($this->sasc) return ($a > $b) ? 1 : -1; else return ($a > $b) ? -1 : 1; } } // }}} }//end of class ?> ---- Server IP: 195.136.184.34 Probable Submitter: 83.15.149.186 ---- Manual Page -- http://www.php.net/manual/en/function.uksort.php Edit -- https://master.php.net/note/edit/71152 Del: integrated -- https://master.php.net/note/delete/71152/integrated Del: useless -- https://master.php.net/note/delete/71152/useless Del: bad code -- https://master.php.net/note/delete/71152/bad+code Del: spam -- https://master.php.net/note/delete/71152/spam Del: non-english -- https://master.php.net/note/delete/71152/non-english Del: in docs -- https://master.php.net/note/delete/71152/in+docs Del: other reasons-- https://master.php.net/note/delete/71152 Reject -- https://master.php.net/note/reject/71152 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119707) next »