note 68689 added to function.array-multisort

From: Date: Mon, 07 Aug 2006 12:53:23 +0000
Subject: note 68689 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-115609@lists.php.net to get a copy of this message
Extending KES's example (http://www.php.net/manual/en/function.array-multisort.php#68452) to look like array_multisort(). NOTE: Fully commented code is available at http://rquadling.php1h.com (sorry for the ads). The syntax is the same as array_multisort(). You also have 3 additional parameters you can use: AMC_SORT_STRING_CASELESS to sort the strings case insensitively. AMC_LOSE_ASSOCIATION (the default behaviour) to lose the associations for the array. AMC_KEEP_ASSOCIATION to keep the associations for the array. Other than that, these function work together JUST like array_multisort but sorts using column(s) without the need to first extract the columns into individual arrays. <?php define ('AMC_SORT_STRING_CASELESS', SORT_STRING + 1); define ('AMC_LOSE_ASSOCIATION', 0); define ('AMC_KEEP_ASSOCIATION', 1); define ('AMC_SORT_ORDER', 0); define ('AMC_SORT_TYPE', 1); function array_multisort_column(array &$a_data, $m_mixed1) { $a_Args = func_get_args(); $i_Args = func_num_args(); $GLOBALS['a_AMC_ordering'] = array(); $a_Columns = array_keys(reset($a_data)); $b_KeepAssociation = False; for($i_Arg = 1 ; $i_Arg < $i_Args ; ) { if (in_array($a_Args[$i_Arg], $a_Columns)) { $s_Column = $a_Args[$i_Arg]; $GLOBALS['a_AMC_ordering'][$a_Args[$i_Arg]] = array ( AMC_SORT_ORDER => SORT_ASC, AMC_SORT_TYPE => SORT_REGULAR, ); while ( isset($a_Args[$i_Arg + 1]) && in_array ( $a_Args[$i_Arg + 1], array ( AMC_KEEP_ASSOCIATION, AMC_LOSE_ASSOCIATION, AMC_SORT_STRING_CASELESS, SORT_ASC, SORT_DESC SORT_NUMERIC, SORT_REGULAR, SORT_STRING, ), True ) ) { if (in_array($a_Args[$i_Arg + 1], array(SORT_ASC, SORT_DESC), True)) { $GLOBALS['a_AMC_ordering'][$s_Column][AMC_SORT_ORDER] = $a_Args[$i_Arg + 1]; } elseif (in_array($a_Args[$i_Arg + 1], array(SORT_REGULAR, SORT_NUMERIC, SORT_STRING, AMC_SORT_STRING_CASELESS), True)) { $GLOBALS['a_AMC_ordering'][$s_Column][AMC_SORT_TYPE] = $a_Args[$i_Arg + 1]; } elseif (AMC_KEEP_ASSOCIATION == $a_Args[$i_Arg + 1]) { $b_KeepAssociation = True; } ++$i_Arg; } } ++$i_Arg; } $s_Sorter = ($b_KeepAssociation ? 'uasort' : 'usort'); $b_Result = $s_Sorter($a_data, 'array_multisort_column_cmp'); unset($GLOBALS['a_AMC_ordering']); return $b_Result; } function array_multisort_column_cmp(array &$a_left, array &$a_right) { $i_Result = 0; foreach($GLOBALS['a_AMC_ordering'] as $s_Column => $a_ColumnData) { switch ($a_ColumnData[AMC_SORT_TYPE]) { case SORT_NUMERIC : $i_ColumnCompareResult = ((intval($a_left[$s_Column]) == intval($a_right[$s_Column])) ? 0 : ((intval($a_left[$s_Column]) < intval($a_right[$s_Column])) ? -1 : 1 ) ); break; case SORT_STRING : $i_ColumnCompareResult = strcmp((string)$a_left[$s_Column], (string)$a_right[$s_Column]); break; case AMC_SORT_STRING_CASELESS : $i_ColumnCompareResult = strcasecmp((string)$a_left[$s_Column], (string)$a_right[$s_Column]); break; case SORT_REGULAR : default : $i_ColumnCompareResult = (($a_left[$s_Column] == $a_right[$s_Column]) ? 0 : (($a_left[$s_Column] < $a_right[$s_Column]) ? -1 : 1 ) ); break; } if (0 == $i_ColumnCompareResult) { continue; } $i_Result = $i_ColumnCompareResult * (($a_ColumnData[AMC_SORT_ORDER] == SORT_DESC) ? -1 : 1); break; } return $i_Result; } ?> ---- Server IP: 66.163.161.117 Probable Submitter: 83.104.121.66 (proxied: 1.1 BANDEXCH) ---- X-Spam-Status: No, hits=3.8 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, UPPERCASE_25_50 autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/68689 Del: integrated -- https://master.php.net/note/delete/68689/integrated Del: useless -- https://master.php.net/note/delete/68689/useless Del: bad code -- https://master.php.net/note/delete/68689/bad+code Del: spam -- https://master.php.net/note/delete/68689/spam Del: non-english -- https://master.php.net/note/delete/68689/non-english Del: in docs -- https://master.php.net/note/delete/68689/in+docs Del: other reasons-- https://master.php.net/note/delete/68689 Reject -- https://master.php.net/note/reject/68689 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115609) next »