天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP Array交叉表實現代碼

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

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

php技術PHP Array交叉表實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 99热在线视频这里只精品 | 国产乱子影视频上线免费观看 | 国产九九九九九九九A片 | 午夜精品久久久久久影视riav | 99国内精品| 国产精品禁18久久久夂久 | ppypp日本欧美一区二区 | 一区三区在线专区在线 | 日韩中文字幕欧美在线视频 | 最近的2019中文字幕HD | 国产成人在线免费观看 | 超清无码波多野吉衣与黑人 | 在线广播收听 | 欧美一级黄色影院 | 好大好硬好爽好深好硬视频 | 国内精品久久久久影院男同志 | 第七色 夜夜撸 | 国产精品第1页在线观看 | www.精品视频 | 亚洲精品久久久久中文字幕二区 | 国产三级在线免费 | 福利视频一二三在线观看 | 国产成人高清精品免费观看 | 国产午夜福利100集发布 | 国产精品伦一区二区三级视频 | 久青草影院 | 成人亚洲乱码在线 | 国产精品爽爽久久久久久竹菊 | 日日做夜夜欢狠狠免费软件 | 成人国产在线看不卡 | 顶级欧美不卡一区二区三区 | 东北女人一级毛片 | 麻豆精品一卡2卡三卡4卡免费观看 | 乌克兰女人与动ZOZO | 久久机热视频 这里只有精品首页 | 97精品少妇偷拍蜜桃AV | 国产精品高清视亚洲一区二区 | 免费99精品国产人妻自在线 | 一本大道无码AV天堂欧美 | 蜜桃久久久亚洲精品成人 | 天美传媒在线完整免费观看网站 |