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也可以?成Shell Script
Oct 09 PHP
php 8小时时间差的解决方法小结
Dec 22 PHP
PHP与SQL注入攻击防范小技巧
Sep 16 PHP
php站内搜索并高亮显示关键字的实现代码
Dec 29 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
Jun 20 PHP
php实现文件下载实例分享
Jun 02 PHP
PHP代码实现爬虫记录――超管用
Jul 31 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
Sep 22 PHP
PHP获取数组中指定的一列实例
Dec 27 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
Dec 21 PHP
PHP pthreads v3下同步处理synchronized用法示例
Feb 21 PHP
PHP实现文件上传操作和封装
Mar 04 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
解析thinkphp的左右值无限分类
2013/06/20 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
2016/11/14 PHP
php观察者模式应用场景实例详解
2017/02/03 PHP
关于document.cookie的使用javascript
2008/04/11 Javascript
Javascript var变量隐式声明方法
2009/10/19 Javascript
去掉gridPanel表头全选框的小例子
2013/07/18 Javascript
jquery日历控件实现方法分享
2014/03/07 Javascript
浅谈JS闭包中的循环绑定处理程序
2014/11/09 Javascript
Javascript连接Access数据库完整实例
2015/08/03 Javascript
JavaScript判断FileUpload控件上传文件类型
2015/09/28 Javascript
jQuery实现右侧显示可向左滑动展示的深色QQ客服效果代码
2015/10/23 Javascript
jquery简单倒计时实现方法
2015/12/18 Javascript
EasyUI 中combotree 默认不能选择父节点的实现方法
2016/11/07 Javascript
js原生代码实现轮播图的实例讲解
2017/07/28 Javascript
Vue2.0利用vue-resource上传文件到七牛的实例代码
2017/07/28 Javascript
理解Koa2中的async&amp;await的用法
2018/02/05 Javascript
Node.js 使用AngularJS的方法示例
2018/05/11 Javascript
echarts实现地图定时切换散点与多图表级联联动详解
2018/08/07 Javascript
详解基于React.js和Node.js的SSR实现方案
2019/03/21 Javascript
layui递归实现动态左侧菜单
2019/07/26 Javascript
记一次用ts+vuecli4重构项目的实现
2020/05/21 Javascript
Vue 使用typescript如何优雅的调用swagger API
2020/09/01 Javascript
js实现购物车商品数量加减
2020/09/21 Javascript
[01:10]3.19DOTA2发布会 三代刀塔人第一代
2014/03/25 DOTA
详解Python操作RabbitMQ服务器消息队列的远程结果返回
2016/06/30 Python
python交互式图形编程实例(二)
2017/11/17 Python
tensorflow 1.0用CNN进行图像分类
2018/04/15 Python
django框架面向对象ORM模型继承用法实例分析
2019/07/29 Python
详解如何从TensorFlow的mnist数据集导出手写体数字图片
2019/08/05 Python
利用CSS3参考手册和CSS3代码生成工具加速来学习网页制
2012/07/11 HTML / CSS
纯CSS绘制漂亮的圆形图案效果
2014/05/07 HTML / CSS
关于css中margin的值和垂直外边距重叠问题
2020/10/27 HTML / CSS
欢迎标语大全
2014/06/21 职场文书
工程部部长岗位职责
2015/02/12 职场文书
中学总务处工作总结
2015/08/12 职场文书
《秋天的怀念》教学反思
2016/02/17 职场文书