Whether in real life or in the programming world, order is always important - I can’t imagine a world without order! PHP arrays are no exception. PHP provides four sets of functions for sorting PHP arrays. The first three are for one-dimensional arrays. Let's talk about these three first. The fourth sorting of multi-dimensional arrays will be discussed in the next article. That one is a bit complicated.
The first group: sort and rsort, sort according to the order asc and reverse order desc of the PHP array key value, while destroying the index relationship of the original array - in fact, re-establishing the numerical index starting from 0 after deleting the index. Take a look at the routine:
<span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,0); FONT-WEIGHT: bold; PADDING-TOP: 0px"><?php</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">=</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">array</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,255); PADDING-TOP: 0px">"a"</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">=></span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">1</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">2</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">sort</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">var_dump</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">rsort</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">var_dump</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,0); FONT-WEIGHT: bold; PADDING-TOP: 0px">?></span> Copy after login |
Look at the first output result, the first output:
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
Second output:
array(2) {
[0]=>
int(5)
[1]=>
int(4)
}
I found that where did the index a that we originally defined go? Where did it go? It can be said for sure that it was ruthlessly deleted by them. If you don’t care about the original index relationship, you can use them!
The second set of functions: asort and arsort. These two functions are more powerful. As long as they can retain the original index relationship of the array, replace the sort and rsort in the above example with these two functions. See Running result:
array(2) {
["a"]=>
int(1)
[0]=>
int(2)
}
array(2) {
[0]=>
int(2)
["a"]=>
int(1)
}
This is easy to understand at a glance, so there’s no need to explain it!
The third group of PHP array sorting functions: krsort and ksort. These two are different from the above two groups. These two functions sort the key names. You can replace the functions in the above example with these two to see the details. I won’t talk about the running results here, otherwise this article would be too long. I’m afraid some brothers won’t have the patience to read the key points of this article, although the key points are below!
Sort PHP arrays through custom functions. There are three functions:
uasort sorts the key values of the PHP array through a custom function and retains the original index relationship.
uksort sorts the key names of the PHP array through a custom function and retains the original index relationship.
usort sorts the key values of the PHP array through a custom function, deletes the original index relationship, and creates a new index from scratch.
This place certainly needs an example:
<span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px"><!--</span>p <span style="PADDING-BOTTOM: 0px; FONT-STYLE: italic; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(102,102,102); PADDING-TOP: 0px">//首先顶一个函数,这个函数需要接受两个参数,并且返回值是一定的</span> <span style="PADDING-BOTTOM: 0px; FONT-STYLE: italic; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(102,102,102); PADDING-TOP: 0px">//第一个参数等于第二个参数的时候返回0,小于的时候返回-1,大于返回1</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,0); FONT-WEIGHT: bold; PADDING-TOP: 0px">function</span> cmp<span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$b</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">{</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">+=</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">1</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$b</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">+=</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">3</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span><span style="PADDING-BOTTOM: 0px; FONT-STYLE: italic; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(102,102,102); PADDING-TOP: 0px">//改变这些值之后进行比较</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(177,177,0); PADDING-TOP: 0px">if</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">==</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$b</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(177,177,0); PADDING-TOP: 0px">return</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">0</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(177,177,0); PADDING-TOP: 0px">return</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">&</span>lt<span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$b</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span>?<span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">-</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">1</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">:</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">1</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">}</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">=</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">array</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">1</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">4</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">3</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(204,102,204); PADDING-TOP: 0px">5</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">uasort</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">,</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,255); PADDING-TOP: 0px">'cmp'</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(153,0,0); PADDING-TOP: 0px">var_dump</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">(</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,0,136); PADDING-TOP: 0px">$a</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(0,153,0); PADDING-TOP: 0px">)</span><span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">;</span> <span style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: rgb(51,153,51); PADDING-TOP: 0px">--></span> Copy after login |
Output result:
array(4) {
[0]=>
int(1)
[3]=>
int(5)
[1]=>
int(4)
[2]=>
int(3)
}
, haha... Is it more messy than not sorting? We are just demonstrating the methods used here. You can do it yourself when you use it! If you do not operate these values before comparing them, for example, here we :
$a+=1;
$b+=3;//Compare after changing these values
Then you might as well just use sort, right?
I won’t talk about the other two. Later, Battlefield will do some experiments on PHP sorting multi-dimensional arrays, and then tell everyone the results and process. Wait a moment!