Home  >  Article  >  Backend Development  >  PHP multidimensional array sorting implementation code_PHP tutorial

PHP multidimensional array sorting implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:45:06816browse

array_multisort
(PHP 4, PHP 5)
array_multisort -- Sort multiple arrays or multidimensional arrays
Description
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] )

Returns TRUE if successful, FALSE if failed.
array_multisort() can be used to sort multiple arrays at once, or to sort multi-dimensional arrays according to one or more dimensions.
Associative (string) key names remain unchanged, but numeric key names will be re-indexed.
The input array is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but very flexible. The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.
Sort order flags:

SORT_ASC - Sort items in ascending order
SORT_DESC - Sort items in descending order

Sort type flags:

SORT_REGULAR - Sort items in normal order Method comparison
SORT_NUMERIC - Compare items based on numeric values ​​
SORT_STRING - Compare items based on strings

Two similar sorting flags cannot be specified after each array. The sort flags specified after each array are valid only for that array - before that, the default values ​​SORT_ASC and SORT_REGULAR.
Example 1. Sort multiple arrays

Copy code The code is as follows:

$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>

After sorting in this example, the first array will contain "10"," a", 100, 100. The second array will contain 1,1,"2",3. The order of the items in the second array is exactly the same as the order of the corresponding items (100 and 100) in the first array.
Copy code The code is as follows:

array(4) {
[0]=> string(2) "10"
[1]=> string(1) "a"
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(1)
[1]=> int(1)
[2]=> string(1) "2"
[3]=> int(3)
}

Example 2. Sort multi-dimensional array
Copy code The code is as follows:

$ar = array (array ("10", 100, 100, "a"), array (1, 3, "2" , 1));
array_multisort ($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
?>

this After sorting in the example, the first array will contain 10, 100, 100, "a" (sorted as a string in ascending order), and the second array will contain 1, 3, "2", 1 (sorted as a numerical value in descending order) .
Example 3. Sorting multi-dimensional array
Copy code The code is as follows:

$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

In this example, after sorting, the first array will become "10", 100, 100, 11, "a" (which is treated as a string in ascending order). The second array will contain 1, 3, "2", 2, 1 (treated as numbers in descending order).
Copy code The code is as follows:

array(2) {
[0]=> array(5) {
[0]=> string(2) "10"
[1]=> int(100)
[2]=> int(100)
[3]= > int(11)
[4]=> string(1) "a"
}
[1]=> array(5) {
[0]=> int (1)
[1]=> int(3)
[2]=> string(1) "2"
[3]=> int(2)
[4 ]=> int(1)
}
}

Example 4. Sorting database results
In this example, each cell in the data array represents a table of a line. This is a typical collection of data recorded in a database.
The data in the example is as follows:
volume | edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
The data are all stored in the array named data. This is usually obtained from the database through a loop, such as mysql_fetch_assoc().
Copy code The code is as follows:

$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 sorted in descending order and the edition will be sorted 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.
Copy code The code is as follows:

// Get the list of columns
foreach ( $data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data in descending order according to volume and in ascending order according to edition
// Use $data as the last parameter and sort by common key
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

Example 5. Case-insensitive sorting
SORT_STRING and SORT_REGULAR are case-sensitive, and uppercase letters will be sorted before lowercase letters.
To perform case-insensitive sorting, sort by lowercase copy of the original array.
Copy code The code is as follows:

$array = array('Alpha', 'atomic ', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
print_r($array );
?>

The above example will output:
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)

[Translator’s Note] This function is quite useful. To help with understanding, please read below This example:
Example 6. Ranking
$grade = array("score" => array(70, 95, 70.0, 60, "70"),
"name" => array("Zhang San", "Li Si", "Wang Wu",
"Zhao Liu", "Liu Qi"));
array_multisort($grade["score"] , SORT_NUMERIC, SORT_DESC,
// Use scores as numerical values, sort from high to low
$grade["name"], SORT_STRING, SORT_ASC);
// Use names as strings, from small to low Large sort
var_dump($grade);
?>
The above example will output:
array(2) {
["score"]=>
array(5 ) {
[0]=>
int(95)
[1]=>
string(2) "70"
[2]=>
float (70)
[3]=>
int(70)
[4]=>
int(60)
}
["name"]=>
array(5) {
[0]=>
string(5) "Li Si"
[1]=>
string(6) "Liu Qi"
[2]=>
string(7) "Wang Wu"
[3]=>
string(9) "Zhang San"
[4]=>
string(8) "Zhao Liu"
}
}
In this example, the array $grade containing grades is sorted from high to low by score. People with the same score are sorted by name. ) sorted from small to large. After sorting, Li Si ranked first with 95 points, and Zhao Liu ranked fifth with 60 points. There is no objection. Zhang San, Wang Wu and Liu Qi all scored 70 points, and their rankings were arranged alphabetically by their names, with Liu first, Wang second and Zhang last. For distinction, the three 70 points are represented by integers, floating point numbers and strings respectively, and their sorted results can be clearly seen in the program output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320429.htmlTechArticlearray_multisort (PHP 4, PHP 5) array_multisort -- Instructions for sorting multiple arrays or multidimensional arrays bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) if...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn