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版(5)
Oct 09 PHP
PHP新手上路(十一)
Oct 09 PHP
珊瑚虫IP库浅析
Feb 15 PHP
dede3.1分页文字采集过滤规则详说(图文教程)
Apr 03 PHP
用PHP实现的四则运算表达式计算实现代码
Aug 02 PHP
解析curl提交GET,POST,Cookie的简单方法
Jun 29 PHP
PHP也能干大事 随机函数
Apr 14 PHP
php基于websocket搭建简易聊天室实践
Oct 24 PHP
PHP表单验证内容是否为空的实现代码
Nov 14 PHP
Smarty3配置及入门语法
Feb 22 PHP
TP5框架实现上传多张图片的方法分析
Mar 29 PHP
php实现简易计算器
Aug 28 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实现更新中间关联表数据的两种方法
2014/09/01 PHP
PHP中的类型约束介绍
2015/05/11 PHP
WordPress中用于创建以及获取侧边栏的PHP函数讲解
2015/12/29 PHP
php实现连接access数据库并转txt写入的方法
2017/02/08 PHP
thinkPHP5分页功能实现方法分析
2017/10/25 PHP
浅析PHP开发规范
2018/02/05 PHP
php学习笔记之字符串常见操作总结
2019/07/16 PHP
通用于ie和firefox的函数 GetCurrentStyle (obj, prop)
2006/12/27 Javascript
跨浏览器开发经验总结(三)   警惕“IE依赖综合症”
2010/05/13 Javascript
JQuery为textarea添加maxlength属性并且兼容IE
2013/04/25 Javascript
使用js操作css实现js改变背景图片示例
2014/03/10 Javascript
iframe里面的元素触发父窗口元素事件的jquery代码
2014/10/19 Javascript
jQuery Form 表单提交插件之formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的应用
2016/01/23 Javascript
jQuery实现优雅的弹窗效果(6)
2017/02/08 Javascript
js 公式编辑器 - 自定义匹配规则 - 带提示下拉框 - 动态获取光标像素坐标
2018/01/04 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
JS闭包原理与应用经典示例
2018/12/20 Javascript
JS原型和原型链原理与用法实例详解
2020/02/05 Javascript
解决Vue 给mapState中定义的属性赋值报错的问题
2020/06/22 Javascript
在Python中使用模块的教程
2015/04/27 Python
Python基于高斯消元法计算线性方程组示例
2018/01/17 Python
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
2018/06/06 Python
Python 字符串、列表、元组的截取与切片操作示例
2019/09/17 Python
Pytorch实现神经网络的分类方式
2020/01/08 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
python实现自动清理重复文件
2020/08/24 Python
Python操作Excel的学习笔记
2021/02/18 Python
缓刑人员的思想汇报
2014/01/11 职场文书
市级青年文明号申报材料
2014/05/26 职场文书
会议接待欢迎标语
2014/10/08 职场文书
初婚未育证明样本
2014/10/24 职场文书
同学聚会邀请函
2015/01/30 职场文书
公务员个人总结
2015/02/12 职场文书
2015年度信用社工作总结
2015/05/04 职场文书
python实现腾讯滑块验证码识别
2021/04/27 Python
Python借助with语句实现代码段只执行有限次
2022/03/23 Python