欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP Array交叉表实现代码

程序员文章站 2022-07-19 17:59:21
如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码 复制代码 代码如下: /** * 基本交叉表 * @author hugh...
如果使用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里面。

然后喜欢怎么输出都可以了