• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP Array交叉表实现代码_php技巧

php 搞代码 3年前 (2022-01-26) 26次浏览 已收录 0个评论

如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码

 <BR>/** <BR>* 基本交叉表 <BR>* @author hugh <BR>* <BR>*/ <BR>class Pivot <BR>{ <BR>private $HORIZONTAL_TOTAL_FIELD = 'total'; <BR>private $VERTICAL_TOTAL_FIELD = 'total'; <BR>private $data; <BR>private $topPivot; <BR>private $leftPivot; <BR>private $measure; <BR>private $horizontalColumn = array (); <BR>private $verticalColumn = array (); <BR>private $pivotValue = array (); <BR>private $isHorizontalTotal = true; <BR>private $isVerticalTotal = true; <BR>private $horizontalTotal = null; <BR>private $verticalTotal = null; <BR>private $title = 'PivotTab'; <BR>/** <BR>* 初始化交叉表 <BR>*/ <BR>private function InitPivot() <BR>{ <BR>$this->topPivot; <BR>foreach ( $this->data as $d ) <BR>{ <BR>$this->horizontalColumn [] = $d [$this->leftPivot]; <BR>$this->verticalColumn [] = $d [$this->topPivot]; <BR>} <BR>$this->horizontalColumn = array_unique ( $this->horizontalColumn ); <BR>$this->verticalColumn = array_unique ( $this->verticalColumn ); <BR>$reasult = array (); <BR>foreach ( $this->horizontalColumn as $h ) <BR>{ <BR>foreach ( $this->verticalColumn as $v ) <BR>{ <BR>$this->pivotValue [$h] [$v] = 0; <BR>} <BR>} <BR>} <BR>/** <BR>* 填充数据 <BR>*/ <BR>private function fillData() <BR>{ <BR>foreach ( $this->data as $row ) <BR>{ <BR>$this->pivotValue [$row [$this->leftPivot]] [$row [$this->topPivot]] += $row [$this<b>/本文来源gao@!dai!ma.com搞$$代^@码5网@</b><strong>搞代gaodaima码</strong>->measure]; <BR>} <BR>if ($this->isHorizontalTotal) <BR>{ <BR>$this->setHorizontalTotal (); <BR>} <BR>if ($this->isVerticalTotal) <BR>{ <BR>$this->setVerticalTotal (); <BR>} <BR>} <BR>/** <BR>* 设置纵向合计 <BR>*/ <BR>private function setVerticalTotal() <BR>{ <BR>$this->verticalColumn [] = $this->VERTICAL_TOTAL_FIELD; <BR>foreach ( $this->horizontalColumn as $i ) <BR>{ <BR>$rowsum = 0; <BR>foreach ( $this->verticalColumn as $j ) <BR>{ <BR>$rowsum += $this->pivotValue [$i] [$j]; <BR>} <BR>$this->pivotValue [$i] [$this->TOTAL_FIELD] = $rowsum; <BR>} <BR>} <BR>/** <BR>* 设置横向合计 <BR>*/ <BR>private function setHorizontalTotal() <BR>{ <BR>$this->horizontalColumn [] = $this->HORIZONTAL_TOTAL_FIELD; <BR>foreach ( $this->verticalColumn as $i ) <BR>{ <BR>$rowsum = 0; <BR>foreach ( $this->horizontalColumn as $j ) <BR>{ <BR>$rowsum += $this->pivotValue [$j] [$i]; <BR>} <BR>$this->pivotValue [$this->HORIZONTAL_TOTAL_FIELD] [$i] = $rowsum; <BR>} <BR>} <BR>/** <BR>* 渲染 <BR>*/ <BR>function Render() <BR>{ <BR>echo '<pre class="prettyprint linenums">'; <BR>print_r ( $this->pivotValue ); <BR>} <BR>/** <BR>* 渲染为table <BR>*/ <BR>function RenderToTable() <BR>{ <BR>$resault = "<table border='1' width='250'>\n"; <BR>$resault .= "<tr><td>$this->title</td>\n"; <BR>foreach ( $this->verticalColumn as $value ) <BR>{ <BR>$resault .= "<td>$value</td>\n"; <BR>} <BR>$resault .= "</tr>\n"; <BR>foreach ( $this->horizontalColumn as $i ) <BR>{ <BR>$resault .= "<tr><td>$i</td>\n"; <BR>foreach ( $this->pivotValue [$i] as $value ) <BR>{ <BR>$resault .= "<td>$value</td>\n"; <BR>} <BR>$resault .= "</tr>\n"; <BR>} <BR>$resault .= "</table>"; <BR>return $resault; <BR>} <BR>/** <BR>* 构造交叉表 <BR>* @param $data 数据源 <BR>* @param $topPivot 头栏目字段 <BR>* @param $leftPivot 左栏目字段 <BR>* @param $measure 计算量 <BR>*/ <BR>function __construct(array $data, $topPivot, $leftPivot, $measure) <BR>{ <BR>$this->data = $data; <BR>$this->leftPivot = $leftPivot; <BR>$this->topPivot = $topPivot; <BR>$this->measure = $measure; <BR>$this->horizontalColumn = array (); <BR>$this->verticalColumn = array (); <BR>$this->InitPivot (); <BR>$this->fillData (); <BR>} <BR>} <BR>


重点在于InitPivot方法及fillData方法。
InitPivot里面保证了所有的item都会有值(默认为0)
fillData方法使用选择填充添加的方法,将数据填充入我们装数据的$pivotValue里面。

然后喜欢怎么输出都可以了


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP Array交叉表实现代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址