Document directory
Add a two-dimensional array ($ sortarray) and sort it. What should we do?
PHP has already completed such a useful function for us: array_multisort
Field1 |
Field2 |
Field3 |
Field4 |
Field5 |
Field6 |
Fieldsort |
Field8 |
Field9 |
1 |
*** |
*** |
*** |
*** |
*** |
3.025 |
*** |
*** |
2 |
*** |
*** |
*** |
*** |
*** |
6.756 |
*** |
*** |
3 |
*** |
*** |
*** |
*** |
*** |
5.375 |
*** |
*** |
4 |
*** |
*** |
*** |
*** |
*** |
12.645 |
*** |
*** |
5 |
*** |
*** |
*** |
*** |
*** |
-6.548 |
*** |
*** |
We need to sort fieldsort in the order of size to small. Then we can first retrieve all values of the fieldsort field and name it $ fieldinarray;
Use the array_multisort function for sorting. The reference code is as follows:
//get fileds in arrayforeach($tempArray as $row) $fieldInArray[] = $row['FIELDSORT'];.........................................//sort tempArrayarray_multisort($fieldInArray,SORT_STRING,SORT_DESC,$tempArray);
In this way, the fieldsort of $ temparray will be sorted in the ascending order.
Definition and usage
The array_multisort () function sorts multiple arrays or multi-dimensional arrays.
The array in the parameter is treated as a table column and sorted by rows-this is similar to the SQL order by statement.
Clause. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.
The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order signs (the sort mark is used to change the default sort order:
- Sort_asc-default, in ascending order. A-Z)
- Sort_desc-sort in descending order. Z-A)
You can then specify the sorting type:
- Sort_regular-default. Sort each item in the general order.
- Sort_numeric-Sort each item in numerical order.
- Sort_string-Sort each item in alphabetical order.
Syntax
array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters |
Description |
Array1 |
Required. Specifies the input array. |
Sorting order |
Optional. Specify the order. Possible values are sort_asc and sort_desc. |
Sorting type |
Optional. Specifies the sorting type. Possible values are sort_regular, sort_numeric, and sort_string. |
Array2 |
Optional. Specifies the input array. |
Array3 |
Optional. Specifies the input array. |
Tips and comments
Note: The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1.
Note: You can set the sorting order and type behind each array. If no value is set, the default value is used for each array parameter.
Example 1
<?php
$a1=array("Dog","Cat");
$a2=array("Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Output:
Array ( [0] => Cat [1] => Dog )
Array ( [0] => Missy [1] => Fido )
Example 2
How to sort two values at the same time:
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,$a2);
print_r($a1);
print_r($a2);
?>
Output:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Fido [2] => Pluto )
Example 3
With sorting parameters:
<?php
$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC,$a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>
Output:
Array ( [0] => Cat [1] => Dog [2] => Dog )
Array ( [0] => Missy [1] => Pluto [2] => Fido )