在实现冒泡排序前,已从数据库获得两个不同栏目下的同类数据。
数据结构如下:
Array
(
[0] => Array
(
[id] => 7
[cid] => 309
[tit] => 栏目一
[listorder] => 7
[uname] => root
[inputtime] => 1478243515
[updatetime] => 1479890692
)
[1] => Array
(
[id] => 10
[cid] => 309
[tit] => 栏目一
[listorder] => 10
[uname] => root
[inputtime] => 1478243720
[updatetime] => 1479890681
)
[2] => Array
(
[id] => 8
[cid] => 309
[tit] => 栏目一
[listorder] => 8
[uname] => root
[inputtime] => 1478243614
[updatetime] => 1479718032
)
)
Array
(
[0] => Array
(
[id] => 11
[cid] => 315
[tit] => 栏目二
[listorder] => 0
[uname] => root
[inputtime] => 1479880613
[updatetime] => 1479970287
)
)
因为是两个不同栏目,所以结果为两个的数组
因此在冒泡之前,我们首先要将两个数组放到同一个数组中。在得到这两个结果前定义一个$arr=array();来存放这两个数组。通过遍历结果集,将两个数组写入$arr中,最终获得一个二维数组:
Array
(
[0] => Array
(
[id] => 7
[cid] => 309
[tit] => 栏目一
[listorder] => 7
[uname] => root
[inputtime] => 1478243515
[updatetime] => 1479890692
)
[1] => Array
(
[id] => 10
[cid] => 309
[tit] => 栏目一
[listorder] => 10
[uname] => root
[inputtime] => 1478243720
[updatetime] => 1479890681
)
[2] => Array
(
[id] => 8
[cid] => 309
[tit] => 栏目一
[listorder] => 8
[uname] => root
[inputtime] => 1478243614
[updatetime] => 1479718032
)
[3] => Array
(
[id] => 11
[cid] => 315
[tit] => 栏目二
[listorder] => 0
[uname] => root
[inputtime] => 1479880613
[updatetime] => 1479970287
)
)
接下来的冒泡就和平常一样
$n=count($arr);
for($j=0;$j<$n-1;$j++){
for($i=0;$i<$n-$j-1;$i++){
//按照更新时间排序
if($arr[$i]['updatetime']<$arr[$i+1]['updatetime']){
$aa = $arr[$i+1];
$arr[$i+1] = $arr[$i];
$arr[$i] = $aa;
}
}
}
最终结果,无论是那个栏目中的内容更新,前台显示时会从所有栏目中按照更新时间进行排序,没有栏目限制。
这个问题类似于之前的替换,也是通过外层的索引数组来对内部的关联数组进行操作。
以上仅为个人观点,欢迎补充!