Home  >  Article  >  Backend Development  >  PHP array array element values ​​sorted in descending order rsort() function_PHP tutorial

PHP array array element values ​​sorted in descending order rsort() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:46955browse

To sort array values, in addition to the various sorting methods taught by the teacher, our PHP directly gives you a function rsort(). The usage is very simple, but it is only suitable for arrays.

rsort(array,sorttype)

Parameter Description
array required. The input array.
sorttype optional. Specifies how to arrange the values ​​of an array. Possible values:

SORT_REGULAR - Default. Processed with their original type (without changing type).
SORT_NUMERIC - treat values ​​as numbers
SORT_STRING - handle values ​​as strings
SORT_LOCALE_STRING - Handle values ​​as strings, based on local settings*.

The code is as follows
 代码如下 复制代码

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
rsort($my_array);
print_r($my_array);
?>

输出:

Array
(
[0] => Horse
[1] => Dog
[2] => Cat
)

Copy 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);

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");

rsort($my_array);

print_r($my_array);

?>

Output:

 代码如下 复制代码
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}
Array

(
[0] => Horse
[1] => Dog
[2] => Cat

)

 代码如下 复制代码

volume | edition
-------+--------
98 |       2
86 |       1
86 |       6
85 |       6
67 |       2
67 |       7

Other references
The code is as follows
Copy 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, volume will be sorted in descending order and 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. //Get the list of columns
The code is as follows Copy code
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, 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:
The code is as follows Copy code
volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7 In fact, there are many methods that can be used when sorting, such as arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort that comes with PHP’s array array. (), usort(), array_multisort() and uksort().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631308.htmlTechArticleFor sorting array values, except that I can use various sorting methods taught by the teacher, our php can directly You have a function rsort(), which is very simple to use, but it is only suitable for arrays. ...
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