一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - PHP教程 - PHP Array交叉表實(shí)現(xiàn)代碼

PHP Array交叉表實(shí)現(xiàn)代碼

2019-11-06 11:01php教程網(wǎng) PHP教程

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

如果使用sql語句做的話 工作量太大了,于是嘗試自己寫一個(gè)交叉表的類,好二話不說,我們看看代碼 

復(fù)制代碼代碼如下:


/** 
* 基本交叉表 
* @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; 



/** 
* 填充數(shù)據(jù) 
*/ 
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 (); 


/** 
* 設(shè)置縱向合計(jì) 
*/ 
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; 


/** 
* 設(shè)置橫向合計(jì) 
*/ 
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; 

/** 
* 構(gòu)造交叉表 
* @param $data 數(shù)據(jù)源 
* @param $topPivot 頭欄目字段 
* @param $leftPivot 左欄目字段 
* @param $measure 計(jì)算量 
*/ 
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 (); 


重點(diǎn)在于InitPivot方法及fillData方法。 
InitPivot里面保證了所有的item都會(huì)有值(默認(rèn)為0) 
fillData方法使用選擇填充添加的方法,將數(shù)據(jù)填充入我們裝數(shù)據(jù)的$pivotValue里面。 

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本视频观看 | 欧美黑人换爱交换乱理伦片 | 成人精品一级毛片 | 91香蕉国产视频 | 丝袜足液精子免费视频 | 亚洲国产精品无码中文字满 | 亚洲AV久久无码精品蜜桃 | 精品一区二区三区视频 | 成年视频在线观看 | 色综合图片 | 四虎永久免费地址ww417 | 四虎最新网址在线观看 | 国产91免费在线 | 我和岳的性事小说 | 四虎最新网址在线观看 | 精品一区二区91 | 国语在线| 色综合色狠狠天天综合色hd | 天选之王漫画顾长歌免费阅读 | 久久爽狠狠添AV激情五月 | 精品视频久久久久 | 69人成网站色www| 免费人成在线观看 | 亚洲网站在线 | 果冻传媒九一制片厂网站 | 国产亚洲视频在线 | 欧美日韩综合网在线观看 | 亚洲伦理一区 | 亚洲国产AV一区二区三区四区 | 日本免费一二区 | 午夜片无码区在线观看 | 嫩草在线视频www免费观看 | 亚欧综合 | 国产区综合另类亚洲欧美 | 女女宿舍互慰h文小说 | 亚洲成aⅴ人片在线 | 秋霞午夜 | 亚洲国产精品久久网午夜小说 | 窝窝影院午夜色在线视频 | 欧美亚洲第一区 | 天天综合色天天综合网 |