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实现的MySQL数据浏览器
Mar 11 PHP
Excel数据导入Mysql数据库的实现代码
Jun 05 PHP
10个实用的PHP代码片段
Sep 02 PHP
PHP中全面阻止SQL注入式攻击分析小结
Jan 30 PHP
ThinkPHP采用实现三级循环代码实例
Jul 18 PHP
php中多维数组按指定value排序的实现代码
Aug 19 PHP
纯PHP代码实现支付宝批量付款
Dec 24 PHP
[原创]php求圆周率的简单实现方法
May 30 PHP
Laravel5.7 数据库操作迁移的实现方法
Apr 12 PHP
yii框架结合charjs统计上一年与当前年数据的方法示例
Apr 04 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
May 15 PHP
php优化查询foreach代码实例讲解
Mar 24 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实现MVC开发得最简单的方法――模型
2007/04/10 PHP
PHP实现文件上传下载实例
2016/10/18 PHP
判断是否输入完毕再激活提交按钮
2006/06/26 Javascript
阻止事件(取消浏览器对事件的默认行为并阻止其传播)
2013/11/03 Javascript
jquery slibings选取同级其他元素的实现代码
2013/11/15 Javascript
Javascript 鼠标移动上去 滑块跟随效果代码分享
2013/11/23 Javascript
Extjs根据条件设置表格某行背景色示例
2014/07/23 Javascript
JS实现文字向下滚动完整实例
2015/02/06 Javascript
jquery实现右键菜单插件
2015/03/29 Javascript
深入学习jQuery Validate表单验证(二)
2016/01/18 Javascript
JS操作COOKIE实现备忘记录的方法
2016/04/01 Javascript
JS iFrame加载慢怎么解决
2016/05/13 Javascript
Bootstrap 3浏览器兼容性问题及解决方案
2017/04/11 Javascript
node.js + socket.io 实现点对点随机匹配聊天
2017/06/30 Javascript
在Vue中使用Compass的方法
2018/03/02 Javascript
详解处理Vue单页面应用SEO的另一种思路
2018/11/09 Javascript
sortable+element 实现表格行拖拽的方法示例
2019/06/07 Javascript
Vue + Element-ui的下拉框el-select获取额外参数详解
2020/08/14 Javascript
JavaScript实现10秒后再次获取验证码
2020/12/02 Javascript
[01:03:00]DOTA2上海特级锦标赛A组败者赛 EHOME VS CDEC第一局
2016/02/25 DOTA
Python实现的简单算术游戏实例
2015/05/26 Python
基于pip install django失败时的解决方法
2018/06/12 Python
对Python使用mfcc的两种方式详解
2019/01/09 Python
java判断三位数的实例讲解
2019/06/10 Python
Python Tornado实现WEB服务器Socket服务器共存并实现交互的方法
2020/05/26 Python
Python 使用office365邮箱的示例
2020/10/29 Python
python+opencv实现车道线检测
2021/02/19 Python
Python用requests库爬取返回为空的解决办法
2021/02/21 Python
萨克斯第五大道精品百货店: Saks Fifth Avenue
2017/04/28 全球购物
澳大利亚在线生活方式商店:Mytopia
2018/07/08 全球购物
学校评语大全
2014/05/06 职场文书
一年级班主任工作总结2014
2014/11/08 职场文书
毕业证明模板
2015/06/19 职场文书
社区低保工作总结2015
2015/07/23 职场文书
SpringCloud的JPA连接PostgreSql的教程
2021/06/26 Java/Android
element tree树形组件回显数据问题解决
2022/08/14 Javascript