First, look at the simplest case. There are two of arrays:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5);
$arr 2 = array (6,2,4);
Array_multisort ($arr 1, $arr 2);
Print_r ($arr 1); The order to get is 1,5,9
Print_r ($arr 2); The order to get is 6,4,2
I estimate that the values of the two arrays correspond from start to finish: 1 corresponds to 2,5 corresponding to 6,9 4.
Let's add one more array to see what happens:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5);
$arr 2 = array (6,2,4);
$arr 3 = array (3,7,8);
Array_multisort ($arr 1, $arr 2, $arr 3);
To view the results, 1 corresponds to 6 corresponding to 3, and so are the other items. This correspondence is what is called in the manual "preserve the original Key name association".
Alternatively, you can think of each array as a column of the database table. and the corresponding 1,6,3 is a data row, 9,2,7 for another data row ...
Array_multisort is sorted by the first array (pictured as a column), sorted by the second array (column) if the value of the first array (column) is the same.
You can use the following program to test:
Copy CodeThe code is as follows:
$arr 1 = array (1,9,5,9);
$arr 2 = array (6,2,4,1);
$arr 3 = array (3,7,8,0);
Array_multisort ($arr 1, $arr 2, $arr 3);
You can imagine the result of $ARR3 here is (3,8,0,7).
second, the next explanation of array_multisort parameters. The parameters of this function are very flexible. The simplest case is that, as shown above, with 1 or n arrays as parameters, it is important to note that the number of items in each array is the same, otherwise warning will cause the sort to fail.
Like this array_multisort ($arr 1, $arr 2, $arr 3); By default, all arrays are sorted in ascending order, and if you want to $arr2 in descending order and compare them as strings, write:
Array_multisort ($arr 1, $arr 2, Sort_desc, sort_string, $arr 3);
Each array can be followed by a sort order flag or a sort type flag, or both flags appear at the same time. However, each sort flag can only appear after each array.
Details are as follows:
Sort order Flags:
Sort_asc-Sort by ascending order (default)
Sort_desc-Sort by descending order
Sort Type flag:
Sort_regular-Compare items by usual method (default)
Sort_numeric-Compare items by value
Sort_string-Compare items by string
third, the last is array_multisort have what practical function.
We usually have some multidimensional arrays that need to be sorted:
$guys = Array
(
[0] = = Array
(
[Name] and Jake
[Score] = 80
[Grade] = A
)
[1] = = Array
(
[Name] = Jin
[Score] = 70
[Grade] = A
)
[2] = = Array
(
[Name] = John
[Score] = 80
[Grade] = A
)
[3] = = Array
(
[Name] = Ben
[Score] = 20
[Grade] = B
)
)
For example, we want to rank in reverse order, if the results are the same, in ascending order by name. Then we need to get two more arrays according to the order of $guys: $scores = Array (80,70,80,20), $names = Array (' Jake ', ' Jin ', ' John ', ' Ben ') and Array_multisort ($scores, Sort_desc, $names, $guys); Can you be more flexible, do you want to sort each other to get some extra arrays? In fact, in the Qeephp Helper_array class has been encapsulated very well, the following is its two methods, the need for people to modify their own can be used:
Copy CodeThe code is as follows:
/**
* Sorts the array according to the specified key
*
Usage
* @code PHP
* $rows = Array (
* Array (' id ' = = 1, ' value ' = ' 1-1 ', ' parent ' = 1),
* Array (' id ' = = 2, ' value ' = ' 2-1 ', ' parent ' = 1),
* Array (' id ' = = 3, ' value ' = ' 3-1 ', ' parent ' = 1),
* Array (' id ' = = 4, ' value ' = ' 4-1 ', ' parent ' = 2),
* Array (' id ' = = 5, ' value ' = ' 5-1 ', ' parent ' = 2),
* Array (' id ' = = 6, ' value ' = ' 6-1 ', ' parent ' = 3),
* );
*
* $rows = Helper_array::sortbycol ($rows, ' id ', SORT_DESC);
* Dump ($rows);
*//Output Result:
*//Array (
*//Array (' id ' = = 6, ' value ' = ' 6-1 ', ' parent ' = + 3),
*//Array (' id ' = = 5, ' value ' = ' 5-1 ', ' parent ' = + 2),
*//Array (' id ' = = 4, ' value ' = ' 4-1 ', ' parent ' = + 2),
*//Array (' id ' = = 3, ' value ' = ' 3-1 ', ' parent ' = + 1),
*//Array (' id ' = = 2, ' value ' = ' 2-1 ', ' parent ' = + 1),
*//Array (' id ' = = 1, ' value ' = ' 1-1 ', ' parent ' = + 1),
* // )
* @endcode
*
* @param array $array to sort
* @param string $keyname sort the key
* @param int $dir sort Direction
*
* @return arrays sorted by array
*/
static function Sortbycol ($array, $keyname, $dir = SORT_ASC)
{
Return Self::sortbymulticols ($array, array ($keyname = $dir));
} /**
* Sort a two-dimensional array in multiple columns, similar to an ORDER by in an SQL statement
*
Usage
* @code PHP
* $rows = Helper_array::sortbymulticols ($rows, Array (
* ' Parent ' = SORT_ASC,
* ' name ' = = Sort_desc,
* ));
* @endcode
*
* @param array $rowset to sort
* @param array $args sorted keys
*
* @return arrays sorted by array
*/
static function Sortbymulticols ($rowset, $args)
{
$sortArray = Array ();
$sortRule = ";
foreach ($args as $sortField = $sortDir)
{
foreach ($rowset as $offset = $row)
{
$sortArray [$sortField] [$offset] = $row [$sortField];
}
$sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir. ', ';
}
if (Empty ($sortArray) | | empty ($sortRule)) {return $rowset;}
Eval (' Array_multisort ('. $sortRule. ' $rowset);
return $rowset;
}
http://www.bkjia.com/PHPjc/322693.html www.bkjia.com true http://www.bkjia.com/PHPjc/322693.html techarticle first, look at the simplest case. There are two arrays: the copy Code code is as follows: $arr 1 = array (1,9,5); $arr 2 = array (6,2,4); Array_multisort ($arr 1, $arr 2); Print_r ($arr 1); Get the Shun ...