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下连接ftp实现文件的上传、下载、删除文件实例代码
Jun 03 PHP
PHP下对数组进行排序的函数
Aug 08 PHP
PHP输出XML到页面的3种方法详解
Jun 06 PHP
PHP jQuery表单,带验证具体实现方法
Feb 15 PHP
php中使用array_filter()函数过滤空数组的实现代码
Aug 19 PHP
双冒号 ::在PHP中的使用情况
Nov 05 PHP
46 个非常有用的 PHP 代码片段
Feb 16 PHP
浅析Yii2 GridView 日期格式化并实现日期可搜索教程
Apr 22 PHP
详解提高使用Java反射的效率方法
Apr 29 PHP
laravel框架数据库操作、查询构建器、Eloquent ORM操作实例分析
Dec 20 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 PHP
THINKPHP5.1 Config的配置与获取详解
Jun 08 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
memcache命令启动参数中文解释
2014/01/13 PHP
PHP多线程类及用法实例
2014/12/03 PHP
实例讲解PHP设计模式编程中的简单工厂模式
2016/02/29 PHP
JavaScript Event学习第二章 Event浏览器兼容性
2010/02/07 Javascript
JavaScript实现QueryString获取GET参数的方法
2013/07/02 Javascript
通过JQuery实现win8一样酷炫的动态磁贴效果(示例代码)
2013/07/13 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
用jquery实现动画跳到顶部和底部(这个比较简单)
2014/09/01 Javascript
JS拖拽插件实现步骤
2015/08/03 Javascript
javascript结合Flexbox简单实现滑动拼图游戏
2016/02/18 Javascript
浅析jquery如何判断滚动条滚到页面底部并执行事件
2016/04/29 Javascript
webuploader模态框ueditor显示问题解决方法
2016/12/27 Javascript
AngularJS页面传参的5种方式
2017/04/01 Javascript
vue2.0模拟锚点的实例
2018/03/14 Javascript
JS实现将二维数组转为json格式字符串操作示例
2018/07/12 Javascript
使用axios发送post请求,将JSON数据改为form类型的示例
2019/10/31 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
压缩Vue.js打包后的体积方法总结(Vue.js打包后体积过大问题)
2020/02/03 Javascript
JavaScript canvas绘制圆弧与圆形
2020/02/18 Javascript
Vue执行方法,方法获取data值,设置data值,方法传值操作
2020/08/05 Javascript
[00:58]PWL开团时刻DAY5——十人开雾0换5
2020/11/04 DOTA
Python heapq使用详解及实例代码
2017/01/25 Python
Python实现的用户登录系统功能示例
2018/02/05 Python
python实现word 2007文档转换为pdf文件
2018/03/15 Python
Python常见工厂函数用法示例
2018/03/21 Python
Tensorflow Summary用法学习笔记
2020/01/10 Python
python 实现socket服务端并发的四种方式
2020/12/14 Python
Canvas环形饼图与手势控制的实现代码
2019/11/08 HTML / CSS
购买200个世界上最好的内衣品牌:Bare Necessities
2017/02/11 全球购物
美国体育用品商店:Academy Sports + Outdoors
2020/01/04 全球购物
C#中的验证控件有几种
2014/03/08 面试题
房产转让协议书
2014/04/11 职场文书
保护环境标语
2014/06/09 职场文书
社区班子对照检查材料
2014/08/27 职场文书
办理房产证委托书
2014/09/18 职场文书
个人学习总结范文
2015/02/15 职场文书