title: Laravel php How to loop through array of arrays and add new key values
body: <p>This is what the array $optvar looks like in response</p>
  
  <pre><code>    0: [{product_id: 18, option_id: 35, value: "green"}, {product_id: 18, option_id: 36, value: "large"}]
      1: [{product_id: 18, option_id: 35, value: "green"}, {product_id: 18, option_id: 36, value: "large"}]
  </code></pre>
  
  <p>I want to add sk_id: $somevalue in each of the inner array;</p>
  
  <p>I tried using foreach loop but only getting the last item when I call it outside the loop;</p>
  
  <p>my loop:</p>
  
  <pre><code>foreach ($optvar as $innerArray) { 
   if (is_array($innerArray)){
  
      foreach ($innerArray as $k =&gt; &amp;$value) {
  
           if($value['option_id'] == $option)
           {
              $value['option_id'] =$var[$key]['value'];
           }
             $value['sku_id'] = $sku-&gt;id;
             $value['created_at'] = $now;
             $value['updated_at'] = $now;
  
  
      } 
   }       
  
  }
  </code></pre>