我正在迭代一个包含 3000 多个项目的数组,数组看起来像这样:
[
[
'id' => 1,
'type' => 'income'
'amount' => 10
],
[
'id' => 2,
'type' => 'expense',
'amount' => 20
],
.......
]
在迭代时,我调用操作同一类中另一个数组的函数,如下所示:
$this->data->revenue->each(function($row) use($user)
{
if ($row->isIncome())
{
$this->addRevenueRowToColumn($row, 'income');
$this->addRevenueRowToColumn($row, 'total');
}
if ($row->isExpense())
{
$this->addRevenueRowToColumn($row, 'expense');
$this->subtractRevenueRowToColumn($row, 'total');
}
}
这就是函数的作用:
protected function addRevenueRowToColumn(&$row, $columnName)
{
$this->report['byMonth'][$row->getMonthTableKey()]['byDepartment'][$row->department_id][$columnName] += $row->amount;
$this->report['byMonth'][$row->getMonthTableKey()]['total'][$columnName] += $row->amount;
$this->report['totals']['byDepartment'][$row->department_id][$columnName] += $row->amount;
$this->report['totals']['total'][$columnName] += $row->amount;
}
protected function subtractRevenueRowToColumn(&$row, $columnName)
{
$this->report['byMonth'][$row->getMonthTableKey()]['byDepartment'][$row->department_id][$columnName] -= $row->amount;
$this->report['byMonth'][$row->getMonthTableKey()]['total'][$columnName] -= $row->amount;
$this->report['totals']['byDepartment'][$row->department_id][$columnName] -= $row->amount;
$this->report['totals']['total'][$columnName] -= $row->amount;
}
处理数据并显示大约需要 11 秒
我应该怎么办?提前致谢!