Note that there are some explanatory texts on larger screens.

plurals
  1. POArray group and some operations
    primarykey
    data
    text
    <p>I have db data like:</p> <pre><code>date, storage, bandwidth, cost </code></pre> <p>I can have entries like:</p> <pre><code>2013-10-24, 1, 0, 0.55; 2013-10-25, 1, 0, 0.50; 2013-10-25, 1, 0, 0.25; 2013-10-25, 0, 1, 0.50; </code></pre> <p>I grab the data from db and make a foreach. I will get of course 4 rows. I will need to group data like this:</p> <p>This means that on 2013-10-24 I had storage cost of 0.55 And on 2013-10-25 I had storage cost of 0.75 and bandwidth cost of 0.50</p> <p>I made a function:</p> <pre> public function sortCostByDay($array, $key) { $return = array(); foreach($array as $v) { $storage = $bandwidth = $total = 0; if($v['storage'] == 1) { $storage += $v['cost']; $total += $storage; } if($v['bandwidth'] == 1) { $bandwidth += $v['cost']; $total += $bandwidth; } $return[$v[$key]][] = array('storage' => $storage, 'bandwidth' => $bandwidth, 'total' => $total); } return $return; } </pre> <p>This will group my initial array by $key. The $key is the date.</p> <p>So I end up having this:</p> <pre><code>'2013-10-24' =&gt; array (size=1) 0 =&gt; array (size=3) 'storage' =&gt; 0.55 'bandwidth' =&gt; 0 'total' =&gt; 0.55 '2013-10-25' =&gt; array (size=3) 0 =&gt; array (size=3) 'storage' =&gt; 0.50 'bandwidth' =&gt; 0 'total' =&gt; 0.50 1 =&gt; array (size=3) 'storage' =&gt; 0.25 'bandwidth' =&gt; 0 'total' =&gt; 0.50 2 =&gt; array (size=3) 'storage' =&gt; 0 'bandwidth' =&gt; 0.50 'total' =&gt; 1 </code></pre> <p>I can`t figure out how to make it be something like:</p> <pre><code>'2013-10-24' =&gt; array (size=1) 0 =&gt; array (size=1) 'storage' =&gt; 0.55 'bandwidth' =&gt; 0 'total' =&gt; 0.55 '2013-10-25' =&gt; array (size=1) 0 =&gt; array (size=3) 'storage' =&gt; 0.75 'bandwidth' =&gt; 0.50 'total' =&gt; 1.25 </code></pre> <p>Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    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