首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 實作交叉表(Pivot Table)類別完整教學

     編輯:PHP 綜合知識

前言

在處理報表資料時,交叉表(Pivot Table)是將原始資料依照行、列維度進行聚合分析的常用手段。若直接用 SQL 撰寫動態交叉查詢,不僅語法複雜,維護成本也極高。本文介紹一個輕量級的 PHP 類別 Pivot,能將二維陣列資料快速轉換為交叉表,支援橫向/縱向小計、多種輸出格式,適合中小型專案快速整合。

核心類別架構

Pivot 類別封裝了初始化、資料填充、合計計算與渲染輸出四大流程,主要屬性如下:

  • $data:輸入資料集(二維關聯陣列)
  • $topPivot:作為列標題的欄位名
  • $leftPivot:作為行標題的欄位名
  • $measure:需聚合計算的數值欄位
  • $horizontalColumn / $verticalColumn:去重後的行、列標題集合
  • $pivotValue:核心儲存結構 [$row][$col] = value
  • $isHorizontalTotal / $isVerticalTotal:是否啟用橫向/縱向小計

關鍵方法解析

1. InitPivot() — 初始化骨架

private function InitPivot() {
    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);

    // 預填 0,確保每個交叉點皆有值
    foreach ($this->horizontalColumn as $h) {
        foreach ($this->verticalColumn as $v) {
            $this->pivotValue[$h][$v] = 0;
        }
    }
}

此方法掃描資料集,取出所有唯一的行、列標題,並預先建立完整的矩陣骨架,避免後續填值時出現「未定義索引」錯誤。

2. fillData() — 資料聚合填充

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();
}

透過累加方式將度量值填入對應交叉點,再依設定呼叫小計計算。

3. 小計計算

  • 縱向小計:在 $verticalColumn 尾端加入 total 欄,逐行累加各列數值。
  • 橫向小計:在 $horizontalColumn 尾端加入 total 列,逐列累加各行數值。

4. 輸出渲染

// 直接輸出陣列結構(除錯用)
function Render() {
    echo '
';
    print_r($this->pivotValue);
}

// 輸出 HTML 表格
function RenderToTable() {
    $html = "\n";
    $html .= "\n";
    foreach ($this->verticalColumn as $v) $html .= "\n";
    $html .= "\n";

    foreach ($this->horizontalColumn as $h) {
        $html .= "\n";
        foreach ($this->pivotValue[$h] as $val) $html .= "\n";
        $html .= "\n";
    }
    $html .= "
{$this->title}$v
$h$val
"; return $html; }

使用範例

$data = [
    ['region' => 'North', 'product' => 'A', 'sales' => 100],
    ['region' => 'North', 'product' => 'B', 'sales' => 200],
    ['region' => 'South', 'product' => 'A', 'sales' => 150],
    ['region' => 'South', 'product' => 'B', 'sales' => 250],
];

$pivot = new Pivot($data, 'product', 'region', 'sales');
$pivot->title = '銷售交叉表';
echo $pivot->RenderToTable();

輸出結果示意

銷售交叉表ABtotal
North100200300
South150250400
total250450700

總結

Pivot 類別以「預建骨架 + 累加填值」的策略,將複雜的交叉邏輯封裝為可重複使用的元件。若專案需求較複雜(如多層維度、動態欄位),可在此基礎上擴充;對於固定維度的報表需求,它已足夠輕量且高效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved