PHP Array交叉表实现代码

前端之家收集整理的这篇文章主要介绍了PHP Array交叉表实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码 @H_301_0@<div class="codetitle"><a style="CURSOR: pointer" data="2903" class="copybut" id="copybut2903" onclick="doCopy('code2903')"> 代码如下:

<div class="codebody" id="code2903"> @H_3010@/** @H3010@* 基本交叉表 @H3010@* @author hugh @H3010@* @H3010@*/ @H3010@class Pivot @H3010@{ @H301_0@private $HORIZONTAL_TOTALFIELD = 'total'; @H301_0@private $VERTICAL_TOTALFIELD = 'total'; @H3010@private $data; @H3010@private $topPivot; @H3010@private $leftPivot; @H3010@private $measure; @H3010@private $horizontalColumn = array (); @H3010@private $verticalColumn = array (); @H3010@private $pivotValue = array (); @H3010@private $isHorizontalTotal = true; @H3010@private $isVerticalTotal = true; @H3010@private $horizontalTotal = null; @H3010@private $verticalTotal = null; @H3010@private $title = 'PivotTab'; @H3010@/** @H3010@* 初始化交叉表 @H3010@*/ @H3010@private function InitPivot() @H3010@{ @H3010@$this->topPivot; @H3010@foreach ( $this->data as $d ) @H3010@{ @H3010@$this->horizontalColumn [] = $d [$this->leftPivot]; @H3010@$this->verticalColumn [] = $d [$this->topPivot]; @H3010@} @H301_0@$this->horizontalColumn = arrayunique ( $this->horizontalColumn ); @H301_0@$this->verticalColumn = arrayunique ( $this->verticalColumn ); @H3010@$reasult = array (); @H3010@foreach ( $this->horizontalColumn as $h ) @H3010@{ @H3010@foreach ( $this->verticalColumn as $v ) @H3010@{ @H3010@$this->pivotValue [$h] [$v] = 0; @H3010@} @H3010@} @H3010@} @H3010@/** @H3010@* 填充数据 @H3010@*/ @H3010@private function fillData() @H3010@{ @H3010@foreach ( $this->data as $row ) @H3010@{ @H3010@$this->pivotValue [$row [$this->leftPivot]] [$row [$this->topPivot]] += $row [$this->measure]; @H3010@} @H3010@if ($this->isHorizontalTotal) @H3010@{ @H3010@$this->setHorizontalTotal (); @H3010@} @H3010@if ($this->isVerticalTotal) @H3010@{ @H3010@$this->setVerticalTotal (); @H3010@} @H3010@} @H3010@/** @H3010@* 设置纵向合计 @H3010@*/ @H3010@private function setVerticalTotal() @H3010@{ @H301_0@$this->verticalColumn [] = $this->VERTICAL_TOTALFIELD; @H3010@foreach ( $this->horizontalColumn as $i ) @H3010@{ @H3010@$rowsum = 0; @H3010@foreach ( $this->verticalColumn as $j ) @H3010@{ @H3010@$rowsum += $this->pivotValue [$i] [$j]; @H3010@} @H301_0@$this->pivotValue [$i] [$this->TOTALFIELD] = $rowsum; @H3010@} @H3010@} @H3010@/** @H3010@* 设置横向合计 @H3010@*/ @H3010@private function setHorizontalTotal() @H3010@{ @H301_0@$this->horizontalColumn [] = $this->HORIZONTAL_TOTALFIELD; @H3010@foreach ( $this->verticalColumn as $i ) @H3010@{ @H3010@$rowsum = 0; @H3010@foreach ( $this->horizontalColumn as $j ) @H3010@{ @H3010@$rowsum += $this->pivotValue [$j] [$i]; @H3010@} @H301_0@$this->pivotValue [$this->HORIZONTAL_TOTALFIELD] [$i] = $rowsum; @H3010@} @H3010@} @H3010@/** @H3010@* 渲染 @H3010@*/ @H3010@function Render() @H3010@{ @H3010@echo '
'; @H301_0@printr ( $this->pivotValue ); @H3010@} @H3010@/** @H3010@* 渲染为table @H3010@*/ @H3010@function RenderToTable() @H3010@{ @H3010@$resault = "\n"; @H3010@$resault .= "<tr><td>$this->title</td>\n"; @H3010@foreach ( $this->verticalColumn as $value ) @H3010@{ @H3010@$resault .= "<td>$value</td>\n"; @H3010@} @H3010@$resault .= "</tr>\n"; @H3010@foreach ( $this->horizontalColumn as $i ) @H3010@{ @H3010@$resault .= "<tr><td>$i</td>\n"; @H3010@foreach ( $this->pivotValue [$i] as $value ) @H3010@{ @H3010@$resault .= "<td>$value</td>\n"; @H3010@} @H3010@$resault .= "</tr>\n"; @H3010@} @H3010@$resault .= "</table>"; @H3010@return $resault; @H3010@} @H3010@/** @H3010@* 构造交叉表 @H3010@* @param $data 数据源 @H3010@* @param $topPivot 头栏目字段 @H3010@* @param $leftPivot 左栏目字段 @H3010@* @param $measure 计算量 @H3010@*/ @H301_0@function _construct(array $data,$topPivot,$leftPivot,$measure) @H3010@{ @H3010@$this->data = $data; @H3010@$this->leftPivot = $leftPivot; @H3010@$this->topPivot = $topPivot; @H3010@$this->measure = $measure; @H3010@$this->horizontalColumn = array (); @H3010@$this->verticalColumn = array (); @H3010@$this->InitPivot (); @H3010@$this->fillData (); @H3010@} @H3010@} @H3010@ @H301_0@重点在于InitPivot方法及fillData方法。 @H_3010@InitPivot里面保证了所有的item都会有值(默认为0) @H301_0@fillData方法使用选择填充添加方法,将数据填充入我们装数据的$pivotValue里面。 然后喜欢怎么输出都可以了

猜你在找的PHP相关文章