Home > Backend Development > PHP Tutorial > Simple method to sort php array_PHP tutorial

Simple method to sort php array_PHP tutorial

WBOY
Release: 2016-07-13 17:14:23
Original
757 people have browsed it

The main requirements of this article introduce the use of PHP's own functions to achieve array sorting, including one-dimensional array ranking, multi-dimensional array sorting, random sorting, etc.

array_multisort($a,$b), $a,$b are two numbers
•The sort() function is used to sort array cells from low to high.
•rsort() function is used to sort array cells from high to low.
•asort() function is used to sort array cells from low to high and maintain index relationship.
•The arsort() function is used to sort the array cells from high to low and maintain the index relationship.
•The ksort() function is used to sort array cells from low to high by key name.
•The krsort() function is used to sort array cells from high to low by key name.

Sort multiple arrays

array_multisort($a,$b), $a,$b are two arrays. If after sorting, the third element of $a array is ranked first, then the third element of $b will not matter His size in $b will be ranked first. Take a look at the results of the program below:

Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
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);
?>

Copy code

 代码如下 复制代码

$array = array('A','2','3','4','5','6','7','8','9','10','J','Q','K');
shuffle($array);//随机排序数组
print_r($array);//输出数组

$ar1 = array("10", 100, 100, "a");

$ar2 = array(1, 3, "2", 1);
代码如下 复制代码

$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);
?>

array_multisort($ar1, $ar2);


var_dump($ar1);

var_dump($ar2);

?>



Randomly sorted
The code is as follows
Copy code

$array = array('A','2','3','4','5','6','7','8','9','10','J ','Q','K');

shuffle($array);//Randomly sort array . Sort multidimensional array
The code is as follows Copy code
$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); ?>
The most commonly used function for array sorting is sort($arr); its function is to sort the array keys in ascending order, and the sorted array key names are no longer the original key names, but the key names reset according to the new array. . And sometimes we require more complex sorting. For example, if you sort by key name, the ksort($arr); function is used here, which will sort the array according to the key name and maintain the original key-value relationship. The corresponding asort($arr); function is to sort by key value and maintain the original key value relationship In the same principle, the rsort(); arsort(); krsort(); function is the same as sort(); rsort(); ksort(); except that the sorting is in descending order. Array operations are a very important foundation of PHP http://www.bkjia.com/PHPjc/628997.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628997.htmlTechArticleThe main requirements of this article introduce the use of PHP's own functions to implement array sorting, including one-dimensional array ranking , multi-dimensional array sorting, random sorting, etc. array_multisort($a,$b), $a,$b is...