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 模拟登陆MSN并获得用户信息
May 16 PHP
PHP session会话的安全性分析
Sep 08 PHP
apache和php之间协同工作的配置经验分享
Apr 08 PHP
php计算当前程序执行时间示例
Apr 24 PHP
PHP内核探索之解释器的执行过程
Dec 22 PHP
php遍历、读取文件夹中图片并分页显示图片的方法
Nov 15 PHP
php实现数据库的增删改查
Feb 26 PHP
PHP+AjaxForm异步带进度条上传文件实例代码
Aug 14 PHP
yii2.0框架使用 beforeAction 防非法登陆的方法分析
Sep 11 PHP
确保Laravel网站不会被嵌入到其他站点中的方法
Oct 18 PHP
PHP开发api接口安全验证操作实例详解
Mar 26 PHP
如何在Mac上通过docker配置PHP开发环境
May 29 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
Zerg建筑一览
2020/03/14 星际争霸
不错的一篇面向对象的PHP开发模式(简写版)
2007/03/15 PHP
php中使用addslashes函数报错问题的解决方法
2013/02/06 PHP
Javascript Jquery 遍历Json的实现代码
2010/03/31 Javascript
基于jquery的复制网页内容到WORD的实现代码
2011/02/16 Javascript
Jquery焦点图实例代码
2014/11/25 Javascript
Ionic快速安装教程
2016/06/03 Javascript
微信小程序 高德地图SDK详解及简单实例(源码下载)
2017/01/11 Javascript
jQuery实现的简单悬浮层功能完整实例
2017/01/23 Javascript
浅谈vue.js导入css库(elementUi)的方法
2018/03/09 Javascript
js经验分享 JavaScript反调试技巧
2018/03/10 Javascript
对vue事件的延迟执行实例讲解
2018/08/28 Javascript
详解babel升级到7.X采坑总结
2019/05/12 Javascript
vue中使用[provide/inject]实现页面reload的方法
2019/09/30 Javascript
selenium+java中用js来完成日期的修改
2019/10/31 Javascript
js实现简单的无缝轮播效果
2020/09/05 Javascript
[01:44]剑指西雅图 展望TI之CIS战队专访
2014/06/25 DOTA
Python实现的二维码生成小软件
2014/07/11 Python
Python3读取zip文件信息的方法
2015/05/22 Python
使用Python脚本和ADB命令实现卸载App
2017/02/10 Python
python中如何使用朴素贝叶斯算法
2017/04/06 Python
Linux上使用Python统计每天的键盘输入次数
2019/04/17 Python
python重要函数eval多种用法解析
2020/01/14 Python
python语言是免费还是收费的?
2020/06/15 Python
Python socket服务常用操作代码实例
2020/06/22 Python
BISSELL官网:北美吸尘器第一品牌
2019/03/14 全球购物
什么是数组名
2012/05/10 面试题
办公室前台岗位职责范本
2013/12/10 职场文书
应届毕业生应聘自荐信范文
2014/02/26 职场文书
护林防火标语
2014/06/27 职场文书
"9.18"国耻日演讲稿范文
2014/09/14 职场文书
员工试用期转正自我评价
2015/03/10 职场文书
2015年店长工作总结范文
2015/04/08 职场文书
新店开张宣传语
2015/07/13 职场文书
2016年小学生迎国庆广播稿
2015/12/18 职场文书
oracle覆盖导入dmp文件的2种方法
2021/05/21 Oracle