Array sorting in PHP has always been a commonplace issue. Let’s focus on the implementation procedures of one-dimensional array and two-dimensional array sorting in PHP. Students can refer to it.
Function: Reorder the array.
Description: Bubble sort (one-dimensional array) (two-dimensional array sorted by a certain key)
Compare the size of the data elements to be sorted pairwise, and if it is found that the order of the two data elements is reversed, swap them until there are no reversed data elements
Imagine the sorted array R[1..N] to be erected vertically, treat each data element as a weighted bubble, scan the array from bottom to top, and scan any light bubble that violates the principle, make it upward." "Float". Repeat this process until the last two qi are the lighter one at the top and the heavier one at the bottom.
The code is as follows |
Copy code |
代码如下 |
复制代码 |
/**
* 冒泡排序 (一维数组)(二维数组某个健排序)
* 两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止
* 设想被排序的数组R[1..N] 垂直竖立,将每个数据元素看作有重量的气泡,从下往上扫描数组,凡扫描违反原则的轻气泡,就使其向上"漂浮".如此反复进行.
* 直到最后任何两个气都是轻者在上,重者在下为止.
*/
function bubble_sort($array,$key=null) {
$count = count($array);
if($count < 0) {
return false;
}
for($i = 0; $i < $count; $i++) {
for($j = $count - 1; $j > $i; $j--) {
if($key && isset($array[$key])){//二维数组健存在
if($array[$j][$key] < $array[$j - 1][$key]) {
$tmp = $array[$j];
$array[$j] = $array[$j - 1];
$array[$j - 1] = $tmp;
}
}else{ //一维数组
if($array[$j] < $array[$j - 1]) {
$tmp = $array[$j];
$array[$j] = $array[$j - 1];
$array[$j - 1] = $tmp;
}
}
}
}
return $array;
}
|
/**<🎜>
* Bubble sort (one-dimensional array) (two-dimensional array sorted by a certain key) <🎜>
* Compare the sizes of the data elements to be sorted pairwise. If it is found that the order of the two data elements is reversed, exchange them until there are no reversed data elements <🎜>
* Imagine that the sorted array R[1..N] is erected vertically, and each data element is regarded as a weighted bubble. The array is scanned from bottom to top. Any light bubbles that violate the principle are scanned and made to "float" upward. .Do this over and over again.<🎜>
* Until the end of any two qi, the lighter one is at the top and the heavier one is at the bottom.<🎜>
*/<🎜>
<🎜>function bubble_sort($array,$key=null) {<🎜>
$count = count($array);<🎜>
If($count < 0) {<🎜>
return false;<🎜>
}<🎜>
for($i = 0; $i < $count; $i++) {<🎜>
for($j = $count - 1; $j > $i; $j--) {
If($key && isset($array[$key])){//The two-dimensional array exists
If($array[$j][$key] < $array[$j - 1][$key]) {<🎜>
$tmp = $array[$j];<🎜>
$array[$j] = $array[$j - 1];<🎜>
$array[$j - 1] = $tmp;<🎜>
}<🎜>
}else{ //One-dimensional array<🎜>
If($array[$j] < $array[$j - 1]) {<🎜>
$tmp = $array[$j];<🎜>
$array[$j] = $array[$j - 1];<🎜>
$array[$j - 1] = $tmp;<🎜>
}<🎜>
}<🎜>
}<🎜>
}<🎜>
return $array;<🎜>
}<🎜>
|
How to use array_multisort sorting
How to use array_multisort() to sort a two-digit array according to the specified key value
In this example, a two-dimensional array named $data is defined and then sorted as follows
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);
// 取得列的列表
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// 将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
print_r($data);
执行后打印结果如下:
Array
(
[0] => Array
(
[volume] => 98
[edition] => 2
)
[1] => Array
(
[volume] => 86
[edition] => 1
)
[2] => Array
(
[volume] => 86
[edition] => 6
[3] => Array
(
[volume] => 85
[edition] => 6
)
[4] => Array
(
[volume] => 67
[edition] => 2
)
[5] => Array
(
[volume] => 67
[edition] => 7
)
)
|
|
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);
// 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, sort by common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
print_r($data);
After execution, the printed result is as follows:
Array
[0] => Array
(
[volume] => 98
[EDITION] = & GT; 2
)
[1] => Array
(
[volume] => 86
[Edition] = & gt; 1
)
[2] => Array
(
[volume] => 86
[edition] =>
[3] => Array
(
[volume] => 85
[edition] =>
)
[4] => Array
(
[volume] => 67
[edition] => 2
)
[5] => Array
(
[volume] => 67
[edition] =>
)
)
To sort a one-dimensional array, we only need to use sort(). The corresponding asort($arr); function is to sort by key value and maintain the original key-value relationship.
Same principle, rsort(); arsort(); krsort(); function is the same as sort(); rsort(); ksort(); except that the sorting is in descending order.
http://www.bkjia.com/PHPjc/631253.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631253.htmlTechArticleArray sorting in PHP has always been a commonplace issue. Let’s focus on one-dimensional sorting in PHP. Students can refer to the implementation program of array and two-dimensional array sorting. Function...