nielbuys / gam-pivot
gonzalo123/gam-pivot的分支
v1.6
2022-10-05 13:11 UTC
README
一个PHP中用于转换表的类
安装方法
composer require nielbuys/gam-pivot
用法示例
以'host'为轴进行转换
$data = Pivot::factory($recordset)
->pivotOn(array('host'))
->addColumn(array('year', 'month'), array('users', 'clicks',))
->fetch();
以'host'为轴进行转换并显示总计
$data = Pivot::factory($recordset)
->pivotOn(array('host'))
->addColumn(array('year', 'month'), array('users', 'clicks',))
->fullTotal()
->lineTotal()
->fetch();
以'host'和'country'为轴进行转换
$data = Pivot::factory($recordset)
->pivotOn(array('host', 'country'))
->addColumn(array('year', 'month'), array('users', 'clicks',))
->fullTotal()
->pivotTotal()
->lineTotal()
->fetch();
以'host'和'country'为轴进行转换并计算列
$averageCbk = function($reg)
{
return round($reg['clicks']/$reg['users'],2);
};
$data = Pivot::factory($recordset)
->pivotOn(array('host', 'country'))
->addColumn(array('year', 'month'), array('users', 'clicks', Pivot::callback('average', $averageCbk)))
->lineTotal()
->pivotTotal()
->fullTotal()
->typeMark()
->fetch();
以‘host’和‘country’为轴进行转换并显示分组计数
$data = Pivot::factory($recordset)
->pivotOn(array('host', 'country'))
->addColumn(array('year', 'month'), array('users', 'clicks', Pivot::count('count')))
->fullTotal()
->pivotTotal()
->lineTotal()
->fetch();
原始记录集
$recordset = array(
array('host' => 1, 'country' => 'fr', 'year' => 2010, 'month' => 1, 'clicks' => 123, 'users' => 4),
array('host' => 1, 'country' => 'fr', 'year' => 2010, 'month' => 2, 'clicks' => 134, 'users' => 5),
array('host' => 1, 'country' => 'fr', 'year' => 2010, 'month' => 3, 'clicks' => 341, 'users' => 2),
array('host' => 1, 'country' => 'es', 'year' => 2010, 'month' => 1, 'clicks' => 113, 'users' => 4),
array('host' => 1, 'country' => 'es', 'year' => 2010, 'month' => 2, 'clicks' => 234, 'users' => 5),
array('host' => 1, 'country' => 'es', 'year' => 2010, 'month' => 3, 'clicks' => 421, 'users' => 2),
array('host' => 1, 'country' => 'es', 'year' => 2010, 'month' => 4, 'clicks' => 22, 'users' => 3),
array('host' => 2, 'country' => 'es', 'year' => 2010, 'month' => 1, 'clicks' => 111, 'users' => 2),
array('host' => 2, 'country' => 'es', 'year' => 2010, 'month' => 2, 'clicks' => 2, 'users' => 4),
array('host' => 3, 'country' => 'es', 'year' => 2010, 'month' => 3, 'clicks' => 34, 'users' => 2),
array('host' => 3, 'country' => 'es', 'year' => 2010, 'month' => 4, 'clicks' => 1, 'users' => 1),
);