php tutorial one-dimensional array sorting and multi-dimensional array sorting
Let’s first look at the examples of one-dimensional data sorting asort() function and ksort() function
//asort() function is based on the ascending order of the array values
$mix = array("Clalei"=>10,"Bill"=>50,"Aala"=>100);
asort($mix);
print_r($mix);
//ksort() function is based on the ascending order of the keywords of the array
$mix = array("Clalei"=>100," Bill"=>50,"Aala"=>10);
ksort($mix);
print_r($mix);
?>
一Dimensional data sorting 2
The sort() function can sort the array in ascending order of letters or numbers (from low to high):
$name = array("Clalei","Bill","Aala");
sort($name);
for($i=0;$i<3;$i++){
echo $name [$i];
}
echo "
";
$price = array(100,50,10);
sort($price);
for ($i=0;$i<3;$i++){
echo $price[$i]." | ";
}
?>
OK Now let’s take a look at the two-dimensional database tutorial sorting example code
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$ data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
In this example, the volume will be arranged in descending order and the edition will be arranged in ascending order.
Now you have an array with rows, but array_multisort() requires an array with columns, so use the following code to get the columns and then sort them.
// Get the list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[ $key] = $row['edition'];
}
// Arrange the data in descending order according to volume, and in ascending order according to edition
// Use $data as the last parameter to generalize Key sorting
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
The data collection is now sorted, and the results are as follows:
volume | edition
--- ----+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Two-dimensional array sorting three
array_multisort() Sort the two-dimensional array
Array $roughData, we intend to sort according to accuracy.
Array( [0] => Array () [username] => 10yl [accuracy] => 0.00 ( [username] => 11yl [accuracy] = > 1.00 ) [2] => Array ( [username] => 12yl [accuracy] => 0.00 ) [3] => Array ( [username] => 13yl [accuracy] => 1.00 ) )
Method:
(1) Extract accuracy column array
foreach ($roughData as $key => $row) {
username'];
$accuracy[$key] = $row['accuracy'];
}
(2) Sort
array_multisort($accuracy, SORT_ASC,$roughData) ;
When print_r($roughData); we will get a two-dimensional array sorted in ascending order of accuracy
Let’s look at a simple example using the two-dimensional array sorting method
$array[] = array("age"=>20,"name"=>"li");
$array[] = array("age"= >21,"name"=>"ai");
$array[] = array("age"=>20,"name"=>"ci");
$array[ ] = array("age"=>22,"name"=>"di");
foreach ($array as $key=>$value){
$age[$ key] = $value['age'];
$name[$key] = $value['name'];
}
array_multisort($age,SORT_NUMERIC,SORT_DESC,$name ,SORT_STRING,SORT_ASC,$array);
print_r($array);
?>