激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP Array交叉表實現代碼

PHP Array交叉表實現代碼

2019-11-06 11:01php教程網 PHP教程

最近需要做到交叉表,而公司的需求比較復雜,一般的交叉表工具都不適合用

如果使用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里面。 

然后喜歡怎么輸出都可以了

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲综合91 | 国产一级免费在线视频 | www.guochan| 成人免费网站在线观看视频 | 免费观看高清视频网站 | 黄色av网站免费 | 久久免费视频一区 | 国产青草视频在线观看视频 | 久久成人国产精品 | 成人性生活视频 | 欧美一区二区三区成人精品 | 久久艹综合| 毛片在线视频免费观看 | 成人免费毛片一 | 黄视频网址| 国产亚洲精品综合一区91 | 国产视频在线观看免费 | 亚洲午夜一区二区三区 | 中文字幕在线观看亚洲 | 中文字幕伦乱 | 欧美成人精品一区 | a级高清免费毛片av在线 | 国产一区二区三区在线免费 | 看片一区| 欧美性生活免费视频 | 成人免费在线视频 | 国产免费v片 | av在线等| 久久久久91视频 | 欧美日韩在线播放一区 | 成人一区二区在线观看视频 | 成人在线97 | 国产久草视频在线 | 免费一级高清毛片 | 久久精品欧美电影 | 一级国产电影 | 成人h精品动漫一区二区三区 | 一区二区三区视频播放 | 久久免费综合视频 | 久久精品资源 | 国产在线精品一区二区不卡 |