一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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交叉表實現代碼,轉載需保留來源!

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

主站蜘蛛池模板: 91成人免费视频 | 伊人久久综在合线亚洲不卡 | 精品国产高清自在线一区二区三区 | 美女黄页视频 | 国产精品永久免费自在线观看 | 亚洲玖玖| 米奇7777狠狠狠狠视频影院 | nnnwww在线观看视频 | 中文字幕免费在线视频 | 99国产高清久久久久久网站 | 亚洲第一成网站 | 一道精品视频一区二区三区图片 | 色老板在线观看视频 | 亚洲一区二区精品视频 | 久久精品99成人中文字幕880 | 色老板在线免费视频 | 国产福利萌白酱喷水视频铁牛 | 夭天色综合 | 久久久毛片| 丁香六月色婷婷 | 男人扒开女人腿使劲桶动态图 | 亚洲91精品麻豆国产系列在线 | 激情一区二区三区成人 | 色婷婷色综合激情国产日韩 | www.精品视频 | 伊人888| 免费777my性欧美另类 | 色偷偷影院| 国产一级做a爰片久久毛片男 | 国产成人精品视频频 | 色版视频在线观看 | 激情综合网站 | 人人澡人人模人人添 | jizz国产视频 | 久久精品视香蕉蕉er大臿蕉 | 日韩中文字幕亚洲无线码 | 天天精品视频在线观看资源 | 真实国产乱子伦久久 | 色婷婷一区二区三区四区成人网 | 四虎永久在线日韩精品观看 | 人成精品视频三区二区一区 |