title: Edit the data of the array inside of the loop - PHP
body: <p>I'm trying to edit the data of the array using the session and loop.</p>
  <p>The expected output in this code is, editing the specific data inside of the loop, but the problem is other data is being edited. Please help me out with this problem, thank you.</p>
  <p>NOTE: If the <code>$cart_action</code> is <code>add</code>, new data will be added, but if the <code>$values['type']</code> is equal to the <code>$_POST['type']</code> the specific quantity of the product will be edited.</p>
  <p>Example, I want to edit the qty of product_id MQFUQ, but I have no idea on how to edit this one, the product_id SHAREZ is always got edited.
  //data
  Array
  (
  [0] =&gt; Array
  (
  [product_number] =&gt; NP1DC
  [product_id] =&gt; 28AHT
  [type] =&gt; SOLOZ
  [qty] =&gt; 1
  )</p>
  <pre><code>    [1] =&gt; Array
          (
              [product_number] =&gt; DZRW8
              [product_id] =&gt; 28AHT
              [type] =&gt; SHAREZ
              [qty] =&gt; 1
          )
  
      [2] =&gt; Array
          (
              [product_number] =&gt; 4G0RY
              [product_id] =&gt; MQFUQ
              [type] =&gt; 
              [qty] =&gt; 2
          )
  
      [3] =&gt; Array
          (
              [product_number] =&gt; ZS1C6
              [product_id] =&gt; 28AHT
              [type] =&gt; SOLOZ
              [qty] =&gt; 1
          )
  
  )
  
  //here's my code
  $result = 0;
  $values_type = &quot;&quot;;
  foreach($_SESSION[&quot;shopping_cart&quot;] as $keys =&gt; $values)
  {
      $values_type = $values['type'];
      if($values[&quot;product_id&quot;] == $_POST[&quot;product_id&quot;] &amp;&amp; $values['type'] == $_POST['type'])
      {
          $item_array = array(
              'product_number'            =&gt;  $values['product_number'],
              'product_id'            =&gt;  $_POST[&quot;product_id&quot;],
              'type'          =&gt;  $_POST[&quot;type&quot;],
              'qty'       =&gt;  $_POST['qty'] + $values['qty']
          );
          $_SESSION[&quot;shopping_cart&quot;][$keys] = $item_array;
          $result = $keys;
          $cart_action = &quot;edit&quot;;
      }
      else {
          $values_type = $values['type'];
          $count = count($_SESSION[&quot;shopping_cart&quot;]);
          $item_array = array(
              'product_number'            =&gt;  $product_number,
              'product_id'            =&gt;  $_POST[&quot;product_id&quot;],
              'type'          =&gt;  $_POST[&quot;type&quot;],
              'qty'       =&gt;  $_POST['qty']
          );
          $result = $count;
          $cart_action = &quot;add&quot;;
      }
  }
  
  if ($cart_action == &quot;add&quot;) {
      $_SESSION[&quot;shopping_cart&quot;][$result] = $item_array;
      echo $cart_action .&quot; &quot;. $values_type;
  }
  elseif ($cart_action == &quot;edit&quot;) {
      $_SESSION[&quot;shopping_cart&quot;][$result] = $item_array;
      echo $cart_action .&quot; &quot;. $values_type;
  }
  else {
      echo &quot;&quot;;
  }
  </code></pre>