我们知道在php数组中分为多维数组与一维数组,我们下面来分别讲述一下php多维数组与一维数组排序原理与实现方法吧。
一维数组
第一组 :sort 和 rsort ,按照PHP数组键值的顺序asc和逆序desc进行排序,同时破坏原来数组的索引关系——其实是删除索引之后重新建立从0开始的数字索引。看一下例程:
代码如下 |
复制代码 |
$a = array("a"=>1,2);
sort($a);
var_dump($a);
rsort($a);
var_dump($a);
?>
看一下第一个输出结果,第一个输出:
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
第二个输出:
array(2) {
[0]=>
int(5)
[1]=>
int(4)
}
|
发现没有我们原来定义的索引a哪里去了?哪里去了?可以肯定的说是被他们无情的删除了,你要是对原来的索引关系并不在意的话,可以使用他们!
第二组函数:asort 和 arsort ,这两个函数就比较厉害一点了,只要他们可以保留数组原有的索引关系,把上例的sort 和 rsort 分别用这两个函数替换一下,看运行结果:
代码如下 |
复制代码 |
array(2) {
["a"]=>
int(1)
[0]=>
int(2)
}
array(2) {
[0]=>
int(2)
["a"]=>
int(1)
}
|
这个一看就明白的,不用说了吧!
第三组PHP数组排序函数:krsort 和 ksort 这两个不同于以上两组,这两函数是对键名进行排序的,大家可以把上例的函数替换成这两个,看看具体运行结果,这里也不说了,不然这个文章写的就太长了,怕有些兄弟没有耐心看到本文的重点,虽然重点就在下边!
通过自定义函数对PHP数组进行排序,有三个函数分别是:
uasort 通过自定义函数对PHP数组的键值进行排序,并且保留原来的索引关系。
uksort 通过自定义函数对PHP数组的键名进行排序,并且保留原来的索引关系。
usort通过自定义函数对PHP数组的键值进行排序,并且删除原来的索引关系,从零开始建立新的索引。
这个地方当然需要一个例子:
代码如下 |
复制代码 |
输出结果:
array(4) {
[0]=>
int(1)
[3]=>
int(5)
[1]=>
int(4)
[2]=>
int(3)
}
|
,
Sort multidimensional array
For example, 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 be ranked first regardless of its position. The size in $b will be ranked first. Take a look at the results of the program below:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$a =array(100,80,50,10,0);
$b = array("c","f","q","e","z");
array_multisort($a,$b);
var_dump($a);
var_dump($b);
?>
运行结果:
array(5) { [0]=> int(0) [1]=> int(10) [2]=> int(50) [3]=> int(80) [4]=> int(100) }
array(5) { [0]=> string(1) “z” [1]=> string(1) “e” [2]=> string(1) “q” [3]=> string(1) “f” [4]=> string(1) “c” }
|
$a =array(100,80,50,10,0);
$b = array("c","f","q","e","z");
array_multisort($a,$b);
var_dump($a);
var_dump($b);
?>
Running result:
array(5) { [0]=> int(0) [1]=> int(10) [2]=> int(50) [3]=> int(80) [4]=> ; int(100) }
array(5) { [0]=> string(1) “z” [1]=> string(1) “e” [2]=> string(1) “q” [3]=> string(1) “f” [4]=> string(1) “c” }
|
Obviously z, which was originally the fifth element of array b, was ranked first!
代码如下 |
复制代码 |
//让我们来构造一个多维数组
$a=array(100,2,4,7,7);
$b=array('ab','ac','ad','ag','ap');
$ab = array($a,$b);
//开始排序
array_multisort($ab[0],SORT_NUMERIC,SORT_DESC,$ab[1],SORT_STRING,SORT_ASC);
print_r($ab);
?>
|
In fact, to make it clear, array_multisort() first sorts the first array according to the size of the key values, and then adjusts the other arrays according to the adjustment strategy of the first array - the third element is placed first , the second element is placed in the second position... - In fact, the most basic embodiment of this multi-dimensional array sorting algorithm!
However, it should be noted that the number of elements in the two arrays must be the same, otherwise a warning message will appear:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in ……
Okay, I hope everyone above can use it. Let’s talk about the main thing: array_multisort() sorts multi-dimensional arrays. This function will be very useful when doing projects in the future!
First, let’s take a look at the operation method of sorting each element [array] of a multi-dimensional array. It is very simple, but there are a few parameters that need to be explained. If you know something about SQL, you will probably understand it at a glance:
The code is as follows |
Copy code |
//Let us construct a multidimensional array<🎜>
$a=array(100,2,4,7,7);<🎜>
$b=array('ab','ac','ad','ag','ap');<🎜>
<🎜>
$ab = array($a,$b);<🎜>
//Start sorting<🎜>
array_multisort($ab[0],SORT_NUMERIC,SORT_DESC,$ab[1],SORT_STRING,SORT_ASC);<🎜>
print_r($ab);<🎜>
?>
|
Explanation: First, we use SORT_NUMERIC to declare that $ab[0] is sorted by numeric type, and SORT_DESC
The declaration order is reverse order (from large to small), and then we sort $ab[1] using the string type, and the order is ascending order (order)
The final sorting result of array $ab is a combination of the two. First, sort in the reverse order of $ab[0]. If there are values of the same size in $ab[0], they will be sorted in the order of $ab[1]. The output result is as follows:
Array (
[0] => Array ( [0] => 100 [1] => 7 [2] => 7 [3] => 4 [4] => 2 )
[1] => Array ( [0] => ab [1] => ag [2] => ap [3] => ad [4] => ac )
)
Is it very similar to using order by in the database? In fact, it’s pretty much the same!
Now let’s look at a more practical example:
The code is as follows
代码如下 |
复制代码 |
$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);
?>
|
|
Copy code
|
$array[] = array("age"=>20,"name"=>"li");
$array[] = array("age"=>21,"name"=>"ai");
$array[] = array("age"=>20,"name"=>"ci");
$array[] = array("age"=>22,"name"=>"di");
代码如下 |
复制代码 |
Array (
[0] => Array ( [age] => 22 [name] => di )
[1] => Array ( [age] => 21 [name] => ai )
[2] => Array ( [age] => 20 [name] => ci )
[3] => Array ( [age] => 20 [name] => li )
) |
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);
?>
The $array[] array in this example is constructed based on the records read from the database. We are now sorting them in order of age from largest to smallest. If the ages are the same, they are sorted in the order of names. This kind of sorting is what we will often use in the future, |
Because the sorting parameter required by array_multisort() must be a column, we use foreach to read out the age and name of this array. What happens next?
Just like the example above, for sorting, you must have seen the last parameter $array. Yes, you need to declare which array to sort, because our first two parameters have nothing to do with the PHP array that needs to be sorted. , although in fact they are the data in $array - the columns we extracted from $array - sorting of course requires columns, and I have never seen row data used for sorting!
The output is as follows - just as we thought:
The code is as follows
| Copy code
|
Array (
[0] => Array ( [age] => 22 [name] => di )
[1] => Array ( [age] => 21 [name] => ai )
[2] => Array ( [age] => 20 [name] => ci )
[3] => Array ( [age] => 20 [name] => li )
)
You see, it’s actually very simple, it’s just that the few parameters that need to be capitalized are a bit annoying! Although it is a bit difficult to understand, it will be good if you understand it. It will be very useful in the future!
Appendix:
Sort order flag:
SORT_ASC – Sort in ascending order
Sort type flag:
SORT_REGULAR – Compare items in the usual way
SORT_NUMERIC – Compare items numerically
SORT_STRING – Compare items by string
http://www.bkjia.com/PHPjc/633095.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633095.htmlTechArticleWe know that PHP arrays are divided into multi-dimensional arrays and one-dimensional arrays. Let’s talk about PHP multi-dimensional arrays respectively. Let’s talk about the principles and implementation methods of one-dimensional array sorting. One-dimensional array The first group...
|