arraynetworks PHP Array交叉表實作程式碼

WBOY
發布: 2016-07-29 08:43:26
原創
1082 人瀏覽過

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

複製代碼 代碼如下:


/**
* 基本交叉表
* @author hugh
*
*/
class Pivot
{
private $HORIZONTAL_TOTAL_FIELD = 'total'
private🎜 ';
private $data;
private $topPivot;
private $leftPivot;
private $measure;
private $horizo​​ntalColumn = array (); private $pivotValue = array ();
private $isHorizo​​ntalTotal = true;
private $isVerticalTotal = true;
private $horizo​​ntalTotal = null]; 🎜>private $title = 'PivotTab';
/**
* 初始化交叉表
*/
private function InitPivot()
{
$this->topPivot;
foreach (this-e >data as $d )
{
$this->horizo​​ntalColumn [] = $d [$this->leftPivot];
$this->verticalColumn [] = $d [$this->topPivot ];
}
$this->horizo​​ntalColumn = array_unique ( $this->horizo​​ntalColumn );
$this->verticalColumn = array_unique ( $this->verticalColumn );
$reasult = $rea );
foreach ( $this->horizo​​ntalColumn 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->isHorizo​​ntalTotal)
{
$this->setHorizo​​ntalTotal ();
}
if ($this->isVerticalTotal)
{
$this->setVerticalTotal ();
}
}
/**
* 設定縱向合計
*/
private function setVerticalTotal()
{
$this-> verticalColumn [] = $this->VERTICAL_TOTAL_FIELD;
foreach ( $this->horizo​​ntalColumn 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 setHorizo​​ntalTotal()
{
$this->horizo​​ntalColumn [] = $🎜>{
$this->horizo​​ntalColumn [] = $🎜>{
$this->horizo​​ntalColumn [] = $🎜>HOZONLDELTAL3_FIL] = $.PR] = $🎜
foreach ( $this->verticalColumn as $i )
{
$rowsum = 0;
foreach ( $this->horizo​​ntalColumn 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";
foreach ( $this->verticalColumn as $value )
{
$resault .= "n";
}
$resault .= "n";
foreach ( $this->horizo​​ntalColumn as $i )
{
$resault . = "n";
foreach ( $this->pivotValue [$i] as $value )
{
$resault .= "n";
}
$resault .= "n";
}
$resault .= "
$this->title$value
$i $value
";
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->horizo​​ntaltalumn = array (Column = array (Column = array (Column = array) );
$this->verticalColumn = array ();
$this->InitPivot ();
$this->fillData ();

}
}


重點在於InitPivot方法及fillData方法。

InitPivot裡面保證了所有的item都會有值(預設為0)

fillData方法使用選擇填充添加的方法,將資料填入我們裝資料的$pivotValue裡面。

然後喜歡怎麼輸出都可以了

以上就介紹了arraynetworks PHP Array交叉表實作程式碼,包含了arraynetworks方面的內容,希望對PHP教學有興趣的朋友有幫助。
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!