note 68689 deleted from function.array-multisort by cmb

From: Date: Sun, 29 Sep 2019 07:48:33 +0000
Subject: note 68689 deleted from function.array-multisort by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212423@lists.php.net to get a copy of this message
Note Submitter: RQuadling at GMail dot com ---- 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', 1001); define ('AMC_KEEP_ASSOCIATION', 1002); define ('AMC_SORT_ORDER', 1003); define ('AMC_SORT_TYPE', 1004); 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; } ?>

« previous php.notes (#212423) next »