- $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
Copy CodeI estimate that the values of the two arrays correspond from start to finish: 1 corresponds to 2,5 corresponding to 6,9 4. Add one more array to see what happens:
- $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);
Copy CodeTo 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. Test:
- $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);
Copy CodeYou can imagine the result of $ARR3 here is (3,8,0,7). Second, the parameters of Array_multisort. 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); The default is that all arrays are in ascending order, if you want to $arr2 descending, and as a string to compare, it should be written: 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. Sort order Flag: 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 numeric value sort_string-Compare items by string Three, array_multisort the actual function usually some multidimensional array need to sort:
- $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
- )
- )
Copy CodeFor the use of PHP array function array_multisort (), you can also read the following articles: PHP array function array_map, Array_multisort multi-dimensional array ordering instance PHP array sorting function Array_ The difference between Multisort and Uasort php array function array_multisort () Usage in PHP array_multisort () Use instances for example, if you want to rank in reverse order, if the results are the same, sort by the ascending 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 of the Helper_array class has been encapsulated very well, the following is its two methods, we use a few changes can be:
- /**
- * 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;
- }
Copy Code |