This function is the first time I read the handbook, did not see what is going on, so it is necessary to record
Usage
BOOL array_multisort ( array &$arr [, mixed $arg = Sort_asc [, mixed $arg = Sort_reg Ular [, mixed $... ]])
array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
The association (string) key name remains the same, but the numeric key name is re-indexed.
Sort order Flags:
SORT_ASC-Sort by ascending order
SORT_DESC-Sort by descending order
Sort Type flag:
SORT_REGULAR-Compare items according to the usual method
SORT_NUMERIC-Compare items by value
SORT_STRING-Compare items by string
You cannot specify two of the same sort flags after each array. The sort flags specified after each array are valid only for that array-the default value SORT_ASC and SORT_REGULAR before.
The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but very flexible. The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.
Example one:
<?phpfunction My_sort ($arrays, $sort _key, $sort _order = sort_asc, $sort _type = sort_numeric) {if (Is_array ($arrays)) {foreach ($arrays as $array) {if (Is_array ($array)) {$key _arrays[] = $array [$sort _key ]; } else {return false; }}} else {return false; } array_multisort ($key _arrays, $sort _order, $sort _type, $arrays); return $arrays;} $person = array (' id ' = = 1, ' name ' = = ' fj ', ' weight ' = ' = ', ' height ' = +), array (' id ' = = 2, ' Name ' = ' tom ', ' weight ' = ' + ', ' height ' and ' max ', Array (' id ' = ' 3 ', ' name ' = ' Jerry ', ' weight ' + ' = 1 The ' height ' and ' 156 ', Array (' id ' = = 4, ' name ' = ' Bill ', ' weight ' = ' = ', ' height ' = ' = '), array (' ID ' = 5, ' name ' = ' linken ', ' weight ' = ' height ' = ' + ', array (' id ' = + 6, ' name ' = ' Madana ', ' W Eight ' = +, ' height ' = ' + ', array (' id ' = + 7, ' name ' =&Gt ' Jordan ', ' weight ' (+), ' height ' = +); $person = My_sort ($person, ' name ', SORT_ASC, sort_string); Var_dump ($per son);? >
Results
Array (size=7) 0 = = Array (size=4) ' id ' + = int 4 ' name ' = = String ' Bill ' (length=4) ' weight ' = = Int ' height ' = + int 1 = = Array (size=4) ' id ' = = int 1 ' name ' = = String ' FJ ' (length=2) ' Weight ' + int ' height ' = + int 2 = = Array (size=4) ' id ' + = int 3 ' name ' = = str ing ' Jerry ' (length=5) ' weight ' + int + ' height ' = = Int 156 3 = = Array (size=4) ' id ' = int 7 ' Name ' = + string ' jordan ' (length=6) ' weight ' = = Int ' height ' + int 4 = AR Ray (size=4) ' ID ' + int 5 ' name ' = + string ' linken ' (length=6) ' weight ' ~ = Int ' height ' = > int 5 + = array (size=4) ' ID ' + int 6 ' name ' = = String ' Madana ' (length=6) ' weight ' = = Int "height" + int 6 = = Array (size=4) ' id ' + int 2 ' name ' = = String ' Tom ' (length=3) ' weight ' = + int ' height ' + int 150
Example two:
<?php$grade = Array ("score" = = Array (+, p, 70.0, (), "+"), "name" = = Array ("Zhang San", "Li Si", "Wan G Wu "," Zhao Liu "," Liu Qi "); Array_multisort ($grade [" Score "], Sort_numeric, sort_desc,//the fraction as a numeric value, sorted from high to low $grade [" Name "], sort_string, SORT_ASC);//name as a string, from small to large sort var_dump ($grade);
Results
Array (size=2) ' score ' = = Array (size=5) 0 + int 1 = String ' "(length=2) 2 = Floa T 3 = int 4 = int + ' name ' = = Array (size=5) 0 = string ' Li Si ' (length=5)
1 = String ' Liu Qi ' (length=6) 2 = String ' Wang Wu ' (length=7) 3 = String ' Zhang San ' (length=9) c16/>4 = String ' Zhao Liu ' (length=8)
In my opinion, this is a more magical function.
/* {{{proto bool Array_multisort (array ar1 [, sort_asc| Sort_desc [, sort_regular| sort_numeric| Sort_string| sort_natural| Sort_flag_case]] [, array ar2 [, sort_asc| Sort_desc [, sort_regular| sort_numeric| Sort_string| sort_natural| Sort_flag_case]]) Sort multiple arrays at once similar to how ORDER by clause works in SQL */php_function (array_multisort) {zval***args;zval* **arrays; Bucket***indirect; Bucket*p; Hashtable*hash;intargc;intarray_size;intnum_arrays = 0;intparse_state[multisort_last]; /* 0-flag not allowed 1-flag allowed */intsort_order = Php_sort_asc;intsort_type = Php_sort_regular;inti, k;if (zend_ Parse_parameters (Zend_num_args () tsrmls_cc, "+", &args, &argc) = = FAILURE) {return;} /* Allocate space for storing pointers to input arrays and sort flags. */arrays = (zval * * *) Ecalloc (argc, sizeof (Zval *)); for (i = 0; i < multisort_last; i++) {parse_state[i] = 0; ARRAYG (Multisort_flags) [i] = (int *) Ecalloc (argc, sizeof (int));} /* Here we go through the input argumentS and parse them. Each one can * is either an array or a sort flag which follows an array. If not * specified, the Sort flags defaults to PHP_SORT_ASC and php_sort_regular * accordingly. There can ' t be both sort flags of the same type after an * array, and the very first argument have to is an array. */for (i = 0; i < argc; i++) {if (z_type_pp (args[i]) = = Is_array) {/* We see the next ARRAY, so We update the sort flag S of * The previous array and reset the sort flags. */if (i > 0) {arrayg (multisort_flags) [multisort_order][num_arrays-1] = Sort_order; ARRAYG (Multisort_flags) [multisort_type][num_arrays-1] = Sort_type;sort_order = Php_sort_asc;sort_type = PHP_SORT_ REGULAR;} arrays[num_arrays++] = args[i];/* Next One is an array or a list of sort flags. */for (k = 0; k < multisort_last; k++) {parse_state[k] = 1;}} else if (Z_TYPE_PP (args[i]) = = Is_long) {switch (Z_LVAL_PP (Args[i]) & ~php_sort_flag_case) {case Php_sort_asc:case PH p_sort_desc:/* flag allowed here */if (parse_State[multisort_order] = = 1) {/* Save the flag and make sure then next ARG was not the current flag. */sort_order = Z_lval_ PP (args[i]) = = Php_sort_desc? -1:1;parse_state[multisort_order] = 0;} else {php_error_docref (NULL tsrmls_cc, e_warning, "Argument #%d is expected to being an array or sorting flag this has not AL Ready been specified ", i + 1); Multisort_abort;} Break;case php_sort_regular:case php_sort_numeric:case php_sort_string:case php_sort_natural: #if HAVE_STRCOLLcase Php_sort_locale_string: #endif/* flag allowed here */if (parse_state[multisort_type] = = 1) {/* Save the flag and make sure Then next ARG was not the current flag. */sort_type = Z_lval_pp (Args[i]);p arse_state[multisort_type] = 0;} else {php_error_docref (NULL tsrmls_cc, e_warning, "Argument #%d is expected to being an array or sorting flag this has not AL Ready been specified ", i + 1); Multisort_abort;} Break;default:php_error_docref (NULL tsrmls_cc, e_warning, "Argument #%d is an unknown sort flag", I + 1); Multisort_abort;breaK;}} else {php_error_docref (NULL tsrmls_cc, e_warning, "Argument #%d is expected to being an array or a sort flag", I + 1); Multisort_abort;}} /* Take care of the last array sort flags. */ARRAYG (Multisort_flags) [multisort_order][num_arrays-1] = Sort_order; ARRAYG (Multisort_flags) [multisort_type][num_arrays-1] = sort_type;/* Make sure the arrays is of the same size. */array_size = Zend_hash_num_elements (z_arrval_pp (arrays[0])); for (i = 0; i < num_arrays; i++) {if (Zend_hash_num_elem Ents (Z_ARRVAL_PP (arrays[i])) = array_size) {php_error_docref (NULL tsrmls_cc, e_warning, "array sizes is inconsistent" ); Multisort_abort;}} /* If All arrays is empty we don ' t need to do anything. */if (Array_size < 1) {for (k = 0; k < multisort_last; k++) {Efree (ARRAYG (Multisort_flags) [K]);} Efree (arrays); Efree (args); Return_true;} /* Create the indirection array. This is an array of size MxN, where * M is the number of entries with each input array and N is the number * of the input array S + 1. The Last ColuMN is NULL to indicate the end * of the row. */indirect = (Bucket * * *) Safe_emalloc (array_size, sizeof (bucket *), 0); for (i = 0; i < array_size; i++) {Indirect[i] = (Bucket * *) Safe_emalloc ((num_arrays + 1), sizeof (bucket *), 0);} for (i = 0; i < num_arrays; i++) {k = 0;for (p = z_arrval_pp (arrays[i])->plisthead; p; p = p->plistnext, k++) {in Direct[k][i] = p;}} for (k = 0; k < array_size; k++) {indirect[k][num_arrays] = NULL;} /* Do the actual sort Magic-bada-bim, bada-boom. */zend_qsort (indirect, array_size, sizeof (Bucket *), php_multisort_compare tsrmls_cc);/* Restructure the arrays based On sorted indirect-this are mostly taken from zend_hash_sort () function. */handle_block_interruptions (); for (i = 0; i < num_arrays; i++) {hash = z_arrval_pp (Arrays[i]); hash->plisthead = ind Irect[0][i];; Hash->plisttail = Null;hash->pinternalpointer = hash->plisthead;for (k = 0; k < array_size; k++) {if (hash-> ;p listtail) {hash->plisttail->plistnext = indirect[K][i];} Indirect[k][i]->plistlast = Hash->plisttail;indirect[k][i]->plistnext = NULL;hash->pListTail = indirect[ K][i];} p = hash->plisthead;k = 0;while (P! = NULL) {if (p->nkeylength = = 0) p->h = k++;p = p->plistnext;} Hash->nnextfreeelement = Array_size;zend_hash_rehash (hash);} Handle_unblock_interruptions ();/* Clean up. */for (i = 0; i < array_size; i++) {Efree (indirect[i]);} Efree (indirect); for (k = 0; k < multisort_last; k++) {Efree (ARRAYG (Multisort_flags) [K]);} Efree (arrays); Efree (args); Return_true;}
Cond
Reference: http://php.net/manual/zh/function.array-multisort.php
Array_multisort the PHP kernel