如果使用sql語句做的話工作量太大了,於是嘗試自己寫一個交叉表的類,好二話不說,我們看看代碼
複製代碼 代碼如下:
/**
* 基本交叉表
* @author hugh
*
*/
class Pivot
{
private $HORIZONTAL_TOTAL_FIELD = 'total'
private🎜 ';
private $data;
private $topPivot;
private $leftPivot;
private $measure;
private $horizontalColumn = array ();
private $isHorizontalTotal = true;
private $isVerticalTotal = true;
private $horizontalTotal = null]; 🎜>private $title = 'PivotTab';
/**
* 初始化交叉表
*/
private function InitPivot()
{
$this->topPivot;
foreach (this-e >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 = $rea );
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->{
$rowsum = 0;
foreach (this $this->vertical)
{
$rowsum += $this->pivotValue [$i] [$j];
}
$this->pivotValue [$i] [$this->TOTAL_FIELD] = $ rowsum;
}
}
/**
* 設定橫向合計
*/
private function setHorizontalTotal()
{
$this->horizontalColumn [] = $🎜>{
$this->horizontalColumn [] = $🎜>{
$this->horizontalColumn [] = $🎜>HOZONLDELTAL3_FIL] = $.PR] = $🎜
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 ''; <br>print_r ( $this->pivotValue ); <br>} <br>print_r ( $this->pivotValue ); <br>} <br>/**<br>* 渲染為table <br>*/ <br>function RenderToTable() <br>{ <br>$resault = "
n";
";
$resault .= "
n"; $this->title n";
foreach ( $this->verticalColumn as $value )
{
$resault .= "$value n";
}
$resault .= "
foreach ( $this->horizontalColumn as $i )
{
$resault . = "
n"; $i n";
foreach ( $this->pivotValue [$i] as $value )
{
$resault .= " $value n";
}
$resault .= "
}
$resault .= "
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->horizontaltalumn = array (Column = array (Column = array (Column = array) );
$this->verticalColumn = array ();
$this->InitPivot ();
$this->fillData ();
InitPivot裡面保證了所有的item都會有值(預設為0)
fillData方法使用選擇填充添加的方法,將資料填入我們裝資料的$pivotValue裡面。然後喜歡怎麼輸出都可以了
以上就介紹了arraynetworks PHP Array交叉表實作程式碼,包含了arraynetworks方面的內容,希望對PHP教學有興趣的朋友有幫助。