Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP | Updating array items through function while iterating - Big performance issue
    primarykey
    data
    text
    <p>i am iterating an array with more than 3000 items in it array looks something like that:</p> <pre><code>[ [ 'id' =&gt; 1, 'type' =&gt; 'income' 'amount' =&gt; 10 ], [ 'id' =&gt; 2, 'type' =&gt; 'expense', 'amount' =&gt; 20 ], ....... ] </code></pre> <p>while iterating i call functions that manipulate another array in the same class something like that:</p> <pre><code>$this-&gt;data-&gt;revenue-&gt;each(function($row) use($user) { if ($row-&gt;isIncome()) { $this-&gt;addRevenueRowToColumn($row, 'income'); $this-&gt;addRevenueRowToColumn($row, 'total'); } if ($row-&gt;isExpense()) { $this-&gt;addRevenueRowToColumn($row, 'expense'); $this-&gt;subtractRevenueRowToColumn($row, 'total'); } } </code></pre> <p>this is what the functions do:</p> <pre><code>protected function addRevenueRowToColumn(&amp;$row, $columnName) { $this-&gt;report['byMonth'][$row-&gt;getMonthTableKey()]['byDepartment'][$row-&gt;department_id][$columnName] += $row-&gt;amount; $this-&gt;report['byMonth'][$row-&gt;getMonthTableKey()]['total'][$columnName] += $row-&gt;amount; $this-&gt;report['totals']['byDepartment'][$row-&gt;department_id][$columnName] += $row-&gt;amount; $this-&gt;report['totals']['total'][$columnName] += $row-&gt;amount; } protected function subtractRevenueRowToColumn(&amp;$row, $columnName) { $this-&gt;report['byMonth'][$row-&gt;getMonthTableKey()]['byDepartment'][$row-&gt;department_id][$columnName] -= $row-&gt;amount; $this-&gt;report['byMonth'][$row-&gt;getMonthTableKey()]['total'][$columnName] -= $row-&gt;amount; $this-&gt;report['totals']['byDepartment'][$row-&gt;department_id][$columnName] -= $row-&gt;amount; $this-&gt;report['totals']['total'][$columnName] -= $row-&gt;amount; } </code></pre> <p>it takes about 11 seconds to process the data and display it</p> <p>what should i do? thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload