How to group items in array by summary each item <pre><code>&lt;?php $quantity = array(1,2,3,4,5,6,7,8,9,10,11,12,1,14,2,16); // Create a new array $output_array = array(); $sum_quantity = 0; $i = 0; foreach ($quantity as $value) { if($sum_quantity &gt;= 35) { $output_array[$i][] = $value; } $sum_quantity = $sum_quantity + $value; } print_r($output_array); </code></pre> <p>When summmary each item >= 35 will auto create child array</p> <pre><code>array( [0] =&gt; array(1, 2, 3, 4, 5, 6, 7), [1] =&gt; array(8, 9, 10), [2] =&gt; array(11, 12, 1), [3] =&gt; array(14, 2, 16) ) </code></pre>