title: Add array values of same array keys in session
body: <p>I have an array structure that is stored in Session</p>
  
  <p><strong>edit</strong> : the number of <code>item_id</code>'s is not definite. It maybe 1-3 items on one transaction and 2-20 items on the next.</p>
  
  <pre><code>"items": [{
      "item_id": "1",
      "item_quantity": "151"
  }, {
      "item_id": "2",
      "item_quantity": "54"
  }, {
      "item_id": "2",
      "item_quantity": "23"
  }, {
      "item_id": "3",
      "item_quantity": "3"
  }, {
      "item_id": "3",
      "item_quantity": "3"
  }]
  </code></pre>
  
  <p>What I'm trying to achieve is to add up all <code>item_quantity</code> which has the same <code>item_id</code></p>
  
  <pre><code>"items": [{
      "item_id": "1",
      "item_quantity": "151"
  }, {
      "item_id": "2",
      "item_quantity": "77"
  }, {
      "item_id": "3",
      "item_quantity": "6"
  }]
  </code></pre>
  
  <p>This is what I have tried so far, but I'm not getting the desired result</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function Save() {
      if (Input::has('addItem')) {
          if (Session::has('items')) {
              Session::push('items', [
                  'item_id' =&gt; Input::get('item_id'),
                  'item_quantity' =&gt; Input::get('item_quantity')
              ]);
              $array = Session::get('items');
  
              foreach($array as $key =&gt; $value) {
                  foreach($value as $item_id =&gt; $item_quantity) {
                      $total = array();
  
                      $id = $value['item_id'];
  
                      $quantity = $value['item_quantity'];
  
                      if (!isset($total[$id])) {
                          $total[$id] = 0;
                      }
  
                      $total[$id] += $quantity;
                      echo $total[$id];
  
                  }
              } else {
                  Session::put('items', [
                      0 =&gt; [
                          'item_id' =&gt; Input::get('item_id'),
                          'item_quantity' =&gt; Input::get('item_quantity')
                      ]
                  ]);
              }
  
              $data = Session::all();
  
              //return $data;
  
              $item = Item::lists('item_name', 'id');
              return View::make('test')-&gt;with('data', $data)-&gt;with('items', $item);
       }
  </code></pre>