note 66731 added to ref.array

From: Date: Sat, 27 May 2006 19:15:06 +0000
Subject: note 66731 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-112378@lists.php.net to get a copy of this message
Public domain, yadda yadda. This is an extension of the assocSort below, patched up a bit. Feedback would be wonderful. <?php function assocMultiSort(&$array,$keys,$directions=true) { if (!is_array($array) || count($array) == 0) return true; if (!is_array($keys)) { $keys = Array($keys); } //we want "current" instead of necessarily the "zeroth" element; there may not be a zeroth element $test = current($array); $assocSortCompare = ''; for ($i=0,$count=count($keys);$i<$count;$i++) { $key = $keys[$i]; if (is_array($directions)) { $direction = $directions[$i]; } else { $direction = $directions; } if ($i > 0) $assocSortCompare .= 'if ($retval != 0) return $retval; '; $assocSortCompare .= '$ax = $a["'.$key.'"]; $bx = $b["'.$key.'"];'; //TODO -- if it's "blank", search up the list until we find something not blank if (is_numeric($test[$key]) || ($test[$key] == ((int)$test[$key]))) { if ($direction) { $assocSortCompare.= ' $retval = ($ax == $bx) ? 0 : (($ax < $bx) ? -1 : 1);'; } else { $assocSortCompare.= ' $retval = ($ax == $bx) ? 0 : (($ax < $bx) ? 1 : -1);'; } } else { if ($direction) { $assocSortCompare.= ' $retval = strcmp($ax,$bx);'; } else { $assocSortCompare.= ' $retval = strcmp($bx,$ax);'; } } } $assocSortCompare.= ' return $retval;'; $assocSortCompare = create_function('$a,$b',$assocSortCompare); $retval = usort($array,$assocSortCompare); return $retval; } ?> ---- Server IP: 66.163.161.117 Probable Submitter: 68.232.253.122 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/note/edit/66731 Del: integrated -- http://master.php.net/note/delete/66731/integrated Del: useless -- http://master.php.net/note/delete/66731/useless Del: bad code -- http://master.php.net/note/delete/66731/bad+code Del: spam -- http://master.php.net/note/delete/66731/spam Del: non-english -- http://master.php.net/note/delete/66731/non-english Del: in docs -- http://master.php.net/note/delete/66731/in+docs Del: other reasons-- http://master.php.net/note/delete/66731 Reject -- http://master.php.net/note/reject/66731 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#112378) next »