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

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

如果使用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;
}
}
}
/**
* 填充數(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 ();
}
}
/**
* 設置縱向合計
*/
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 數(shù)據(jù)源
* @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方法使用選擇填充添加的方法,將數(shù)據(jù)填充入我們裝數(shù)據(jù)的$pivotValue里面。

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

php技術PHP Array交叉表實現(xiàn)代碼,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 两性午夜色视频免费网站 | 一级毛片两人添下面 | 总攻催眠受的高h巨肉np | 色婷婷五月综合中文字幕 | av淘宝 在线观看 | 久久精品熟女亚洲AV国产 | 精品国产国偷自产在线观看 | 亚洲国产cao| 91精品国产高清久久久久久 | 亚洲国产高清在线 | 午夜伦yy44880影院 | 乳色吐息在线观看全集免费观看 | 好湿好滑好硬好爽好深视频 | 广播电台在线收听 | 70岁妇女牲交色牲片 | 精品无码久久久久久久动漫 | 99视频精品国产免费观看 | 97在线国内自拍视频 | 亚洲一卡久久4卡5卡6卡7卡 | GOGOGO高清在线播放免费 | 手机在线亚洲日韩国产 | 国产一区日韩二区欧美三区 | 99精品视频在线免费观看 | 午夜男人免费福利视频 | 亚洲日本在线不卡二区 | 欧美亚洲国产专区在线 | 老司机午夜影院味味 | 老太脱裤子让老头玩xxxxx | 快播官方网站 | 高清视频在线观看SEYEYE | 国产精品久久久久影院免费 | 日本漂亮妈妈7观整有限中 日本片bbbxxx | 亚洲国产精品线在线观看 | 亚洲不卡高清免v无码屋 | 儿子操妈妈 | 91福利在线观看 | 日本美国群交P片内射捆绑 日本毛片久久国产精品 | 午夜啪啪免费视频 | 青柠视频在线观看高清HD | 久久91精品久久久久久水蜜桃 | 在线成人精品国产区免费 |