PHP Array交叉表实现代码


Posted in PHP onAugust 05, 2010

如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码

/** 
* 基本交叉表 
* @author hugh 
* 
*/ 
class Pivot 
{ 
private $HORIZONTAL_TOTAL_FIELD = 'total'; 
private $VERTICAL_TOTAL_FIELD = 'total'; 
private $data; 
private $topPivot; 
private $leftPivot; 
private $measure; 
private $horizontalColumn = array (); 
private $verticalColumn = array (); 
private $pivotValue = array (); 
private $isHorizontalTotal = true; 
private $isVerticalTotal = true; 
private $horizontalTotal = null; 
private $verticalTotal = null; 
private $title = 'PivotTab'; 
/** 
* 初始化交叉表 
*/ 
private function InitPivot() 
{ 
$this->topPivot; 
foreach ( $this->data as $d ) 
{ 
$this->horizontalColumn [] = $d [$this->leftPivot]; 
$this->verticalColumn [] = $d [$this->topPivot]; 
} 
$this->horizontalColumn = array_unique ( $this->horizontalColumn ); 
$this->verticalColumn = array_unique ( $this->verticalColumn ); 
$reasult = array (); 
foreach ( $this->horizontalColumn as $h ) 
{ 
foreach ( $this->verticalColumn as $v ) 
{ 
$this->pivotValue [$h] [$v] = 0; 
} 
} 
} 
/** 
* 填充数据 
*/ 
private function fillData() 
{ 
foreach ( $this->data as $row ) 
{ 
$this->pivotValue [$row [$this->leftPivot]] [$row [$this->topPivot]] += $row [$this->measure]; 
} 
if ($this->isHorizontalTotal) 
{ 
$this->setHorizontalTotal (); 
} 
if ($this->isVerticalTotal) 
{ 
$this->setVerticalTotal (); 
} 
} 
/** 
* 设置纵向合计 
*/ 
private function setVerticalTotal() 
{ 
$this->verticalColumn [] = $this->VERTICAL_TOTAL_FIELD; 
foreach ( $this->horizontalColumn as $i ) 
{ 
$rowsum = 0; 
foreach ( $this->verticalColumn as $j ) 
{ 
$rowsum += $this->pivotValue [$i] [$j]; 
} 
$this->pivotValue [$i] [$this->TOTAL_FIELD] = $rowsum; 
} 
} 
/** 
* 设置横向合计 
*/ 
private function setHorizontalTotal() 
{ 
$this->horizontalColumn [] = $this->HORIZONTAL_TOTAL_FIELD; 
foreach ( $this->verticalColumn as $i ) 
{ 
$rowsum = 0; 
foreach ( $this->horizontalColumn as $j ) 
{ 
$rowsum += $this->pivotValue [$j] [$i]; 
} 
$this->pivotValue [$this->HORIZONTAL_TOTAL_FIELD] [$i] = $rowsum; 
} 
} 
/** 
* 渲染 
*/ 
function Render() 
{ 
echo '<pre>'; 
print_r ( $this->pivotValue ); 
} 
/** 
* 渲染为table 
*/ 
function RenderToTable() 
{ 
$resault = "<table border='1' width='250'>\n"; 
$resault .= "<tr><td>$this->title</td>\n"; 
foreach ( $this->verticalColumn as $value ) 
{ 
$resault .= "<td>$value</td>\n"; 
} 
$resault .= "</tr>\n"; 
foreach ( $this->horizontalColumn as $i ) 
{ 
$resault .= "<tr><td>$i</td>\n"; 
foreach ( $this->pivotValue [$i] as $value ) 
{ 
$resault .= "<td>$value</td>\n"; 
} 
$resault .= "</tr>\n"; 
} 
$resault .= "</table>"; 
return $resault; 
} 
/** 
* 构造交叉表 
* @param $data 数据源 
* @param $topPivot 头栏目字段 
* @param $leftPivot 左栏目字段 
* @param $measure 计算量 
*/ 
function __construct(array $data, $topPivot, $leftPivot, $measure) 
{ 
$this->data = $data; 
$this->leftPivot = $leftPivot; 
$this->topPivot = $topPivot; 
$this->measure = $measure; 
$this->horizontalColumn = array (); 
$this->verticalColumn = array (); 
$this->InitPivot (); 
$this->fillData (); 
} 
}

重点在于InitPivot方法及fillData方法。
InitPivot里面保证了所有的item都会有值(默认为0)
fillData方法使用选择填充添加的方法,将数据填充入我们装数据的$pivotValue里面。

然后喜欢怎么输出都可以了

PHP 相关文章推荐
PHP入门速成教程
Mar 19 PHP
PHP小程序自动提交到自助友情连接
Nov 24 PHP
PHP对接微信公众平台消息接口开发流程教程
Mar 25 PHP
php修改指定文件后缀的方法
Sep 11 PHP
php几个预定义变量$_SERVER用法小结
Nov 07 PHP
PHP在线调试执行的实现方法(附demo源码)
Apr 28 PHP
深入分析PHP优化及注意事项
Jul 04 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
Oct 10 PHP
PHP对称加密函数实现数据的加密解密
Oct 27 PHP
PHP对象实例化单例方法
Jan 19 PHP
postman的安装与使用方法(模拟Get和Post请求)
Aug 06 PHP
Laravel5.5 数据库迁移:创建表与修改表示例
Oct 23 PHP
php垃圾代码优化操作代码
Aug 05 #PHP
PHP MemCached 高级缓存应用代码
Aug 05 #PHP
phpMyAdmin 链接表的附加功能尚未激活的问题
Aug 01 #PHP
PHP合并数组+与array_merge的区别分析
Aug 01 #PHP
PHP自定义函数收代码
Aug 01 #PHP
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装最快的解决办法
Aug 01 #PHP
PHP5中使用PDO连接数据库的方法
Aug 01 #PHP
You might like
php 不使用js实现页面跳转
2014/02/11 PHP
Linux系统下使用XHProf和XHGui分析PHP运行性能
2015/12/08 PHP
PHP编写RESTful接口的方法
2016/02/21 PHP
PHP实现可自定义样式的分页类
2016/03/29 PHP
实例化php类时传参的方法分析
2020/06/05 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
js split 的用法和定义 js split分割字符串成数组的实例代码
2012/05/13 Javascript
js报$ is not a function 的问题的解决方法
2014/01/20 Javascript
jquery向上向下取整适合分页查询
2014/09/06 Javascript
js漂浮广告实现代码
2015/08/15 Javascript
jquery动态导航插件dynamicNav用法实例分析
2015/09/06 Javascript
JavaScript在网页中画圆的函数arc使用方法
2015/11/13 Javascript
个人网站留言页面(前端jQuery编写、后台php读写MySQL)
2016/05/03 Javascript
jQuery web 组件 后台日历价格、库存设置的代码
2016/10/14 Javascript
JS中parseInt()和map()用法分析
2016/12/16 Javascript
jQuery插件HighCharts绘制2D带Label的折线图效果示例【附demo源码下载】
2017/03/08 Javascript
ES6中Math对象新增的方法实例详解
2017/04/25 Javascript
解决修复npm安装全局模块权限的问题
2018/05/17 Javascript
详解Vue-cli中的静态资源管理(src/assets和static/的区别)
2018/06/19 Javascript
JS实现简易留言板特效
2019/12/23 Javascript
压缩Vue.js打包后的体积方法总结(Vue.js打包后体积过大问题)
2020/02/03 Javascript
vue 微信分享回调iOS和安卓回调出现错误的解决
2020/09/07 Javascript
[01:04:30]Fnatic vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python缩进区别分析
2014/02/15 Python
python使用三角迭代计算圆周率PI的方法
2015/03/20 Python
简单的Python2.7编程初学经验总结
2015/04/01 Python
Python使用re模块实现信息筛选的方法
2018/04/29 Python
python实现石头剪刀布小游戏
2021/01/20 Python
在CentOS7下安装Python3教程解析
2020/07/09 Python
英国香水店:The Perfume Shop
2017/03/27 全球购物
美国孩之宝玩具官网:Hasbro Pulse
2019/06/24 全球购物
化学专业毕业生自荐信
2013/11/15 职场文书
三项教育活动实施方案
2014/03/30 职场文书
个人查摆问题及整改措施
2014/10/16 职场文书
单位考核鉴定意见
2015/06/05 职场文书
mysql通过group by分组取最大时间对应数据的两种有效方法
2022/09/23 MySQL