php array_multisort对数据库结果多个字段进行排序
2015-10-24 13:18
656 查看
php array_multisort对数据库结果多个字段进行排序
$data 数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。
例子中的数据如下:
volume | edition -------+-------- 67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7
数据全都存放在名为 data 的数组中。这通常是通过循环从数据库取得的结果,例如 mysql_fetch_assoc()。
<?php
$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);
?>
本例中将把 volume 降序排列,把 edition 升序排列。
现在有了包含有行的数组,但是 array_multisort() 需要一个包含列的数组,因此用以下代码来取得列,然后排序。
<?php
// 取得列的列表
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);
?>
数据集合现在排好序了,结果如下:
volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7
====================================================
实例2:
//有优惠活动优先 + 上架时间 最新时间的在最上面
//根据商品id取出来然后在用数组排序array_multisort
foreach ($goods as $key => $row) {
$start_time[$key] = $row['goods_listing_start_time'];
$is_activity[$key] = $row['is_activity'];
}
//SORT_ASC SORT_DESC
array_multisort($is_activity,SORT_DESC,$start_time, SORT_DESC, $goods);
$data 数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。
例子中的数据如下:
volume | edition -------+-------- 67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7
数据全都存放在名为 data 的数组中。这通常是通过循环从数据库取得的结果,例如 mysql_fetch_assoc()。
<?php
$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);
?>
本例中将把 volume 降序排列,把 edition 升序排列。
现在有了包含有行的数组,但是 array_multisort() 需要一个包含列的数组,因此用以下代码来取得列,然后排序。
<?php
// 取得列的列表
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);
?>
数据集合现在排好序了,结果如下:
volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7
====================================================
实例2:
//有优惠活动优先 + 上架时间 最新时间的在最上面
//根据商品id取出来然后在用数组排序array_multisort
foreach ($goods as $key => $row) {
$start_time[$key] = $row['goods_listing_start_time'];
$is_activity[$key] = $row['is_activity'];
}
//SORT_ASC SORT_DESC
array_multisort($is_activity,SORT_DESC,$start_time, SORT_DESC, $goods);
相关文章推荐
- PHP 读取文件的主要方法
- 50. PHP 页面静态化(3)
- PHP设计模式四:适配器模式
- PHP7新特性整理介绍篇
- PHP 高级编程之多线程-消息队列
- PHP错误Warning:mysql_query()解决方法
- FTP协议过程分析
- php实现表单多按钮提交action的处理方法
- php中Password Hashing加密方法详解
- PHP之多进程
- php留言本
- 解析PHP缓存函数的使用说明
- PHP 性能分析(一): XHProf & XHGui 介绍
- 如何自建FTP站点
- PHP 中 9 大缓存技术总结
- 10个典型实用的PHP代码片段
- ThinkPhp3.2.3完全开发手册(在线版)
- 关于使用phpstudy搭建本地服务器,80端口被系统服务占用,不能关掉
- Mac下通过 brew 安装不同版本的php
- php实现表单多按钮提交action的处理方法