PHP Array交叉表实现代码_PHP
如果使用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 '
'; <br>print_r ( $this->pivotValue ); <br>} <br>/** <br>* 渲染为table <br>*/ <br>function RenderToTable() <br>{ <br>$resault = "
$this->title | \n";$value | \n";
$i | \n";$value | \n";
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里面。
然后喜欢怎么输出都可以了

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

標題:C#中使用Array.Sort函數對陣列進行排序的範例正文:在C#中,陣列是一種常用的資料結構,經常需要對陣列進行排序運算。 C#提供了Array類,其中有Sort方法可以方便地對陣列進行排序。本文將示範如何使用C#中的Array.Sort函數對陣列進行排序,並提供具體的程式碼範例。首先,我們要先了解Array.Sort函數的基本用法。 Array.So

在進行PHP編程時,我們常常需要將數組合併。 PHP提供了array_merge()函數來完成數組合併的工作,但是當數組中存在相同的鍵時,函數會覆寫原來的值。為了解決這個問題,PHP在語言中還提供了一個array_merge_recursive()函數,該函數可以合併數組並保留相同鍵的值,使得程式的設計變得更加靈活。 array_merge

在PHP中,有許多強大的陣列函數可以讓陣列的操作更加方便和快速。當我們需要將兩個陣列拼成一個關聯數組時,可以使用PHP的array_combine函數來實現這一操作。這個函數其實是用來將一個陣列的鍵當作另一個陣列的值,合併成一個新的關聯數組。接下來,我們將會講解如何使用PHP中的array_combine函數將兩個陣列拼成關聯數組。了解array_comb

在PHP程式設計中,陣列是一種非常重要的資料結構,能夠輕鬆處理大量資料。 PHP中提供了許多陣列相關的函數,array_fill()就是其中之一。本篇文章將詳細介紹array_fill()函數的用法,以及在實際應用上的一些技巧。一、array_fill()函數概述array_fill()函數的作用是建立一個指定長度的、由相同的值組成的陣列。具體來說,該函數的語法

Java是一種非常強大的程式語言,廣泛應用於各種開發領域。但是,在Java程式設計過程中,開發人員常會遇到ArrayIndexOutOfBoundsException異常。那麼,這個異常的常見原因是什麼呢? ArrayIndexOutOfBoundsException是Java中常見的一個執行時期例外。它表示在存取資料時,數組下標超出了數組的範圍。常見的原因包括以

在PHP程式設計中,陣列是一個常用到的資料型別。而關於陣列的運算子也是相當多的,其中包含了array_change_key_case()函數。這個函數可以將數組中鍵名的大小寫轉換,從而方便我們進行資料的處理。本文就來介紹PHP中array_change_key_case()函數的使用方法。一、函數語法及參數array_change_ke

LinkedList類別的toArray()方法將目前的LinkedList物件轉換為物件類型的陣列並傳回它。此數組按正確順序(從第一個元素到最後一個元素)包含此列表中的所有元素。它充當基於數組和基於集合的API之間的橋樑。因此,將LinkedList轉換為陣列-實例化LinkedList類別。使用add()方法填充它。呼叫上面建立的鍊錶上的toArray()方法並檢索物件數組。將物件數組的每個元素轉換為字串。範例 即時示範importjava.util.Arrays;importjava.uti

Python中的array模組是一個預先定義的數組,因此其在記憶體中佔用的空間比標準列表小得多,同時也可以執行快速的元素級別操作,例如添加、刪除、索引和切片等操作。此外,數組中的所有元素都是同一種類型,因此可以使用數組提供的高效數值運算函數,例如計算平均值、最大值和最小值等。另外,array模組還支援將數組物件直接寫入和讀取到二進位檔案中,這使得在處理大量數值資料時更加有效率。因此,如果您需要處理大量同質數據,可以考慮使用Python的array模組來優化程式碼的執行效率。要使用array模組,首先需要
