title: How to update the data in an array with a specific array key?
body: <p>I am creating a cart using session and 2D array my problem is i want to change the content of one of the array in 2D array and i have an order id just to make sure that it changes the correct array, here's my code please help me;</p>
  
  <p>This is the code for inserting new item:</p>
  
  <pre><code>if(isset($_POST['addtocart'])){
      $count= count($_SESSION['cart']);
      $newproduct= array(
          'id' =&gt;  $count,
          'code' =&gt; $_SESSION['code'],
          'color' =&gt; $_POST['color'],
          'type' =&gt; $_POST['type'],
          'size' =&gt;$_POST['size'],
          'quantity' =&gt; 1
      );
      $_SESSION['cart'][]= $newproduct;
  }
  </code></pre>
  
  <p><code>$_SESSION['cart']</code> is the name of my 2D array. Now here's the sample list:</p>
  
  <pre><code>$_SESSION['cart']=&gt; array([0] =&gt;(array
  order=&gt; 0,  
  code=&gt; 123,  
  color=&gt; blue,  
  type=&gt; us,  
  size=&gt; M.  
  quantity =&gt; 1 );
  ([1] =&gt;(array
  order=&gt; 1,  
  code=&gt; 125,  
  color=&gt; red,  
  type=&gt; uk,  
  size=&gt; S.  
  quantity =&gt; 1 ); 
  </code></pre>
  
  <p>Now I want to update the quantity of a the array with an id of 125, but supposed I don't know the id. In my cart list I have a selectbox that automatically submits when an option is selected. Here's my code;  </p>
  
  <pre><code>    &lt;select name="quantity1" id="quantity" onchange="javascript: submit()" &gt;
  &lt;option value="&lt;?php echo $item['quantity']; ?&gt;"&gt;&lt;?php echo $item['quantity']; ?&gt;&lt;/option&gt;
  &lt;?php
      $sql2 = "SELECT * from color_variation 
      WHERE  color='$color' 
      AND size='$size' 
      AND size_type='$type'";
      $q2= $conn-&gt;query($sql2);
      $q2-&gt;setFetchMode (PDO::FETCH_ASSOC);
      while($r2 = $q2-&gt;fetch())
      {
          $x= $r2['quantity'];
      }
      for($y=1; $y&lt;=$x; $y++){
          if($y== $r1['quantity']){
  
          } else{
  ?&gt;
  &lt;option value="&lt;?php echo $y; ?&gt;"&gt;&lt;?php echo $y; ?&gt;&lt;/option&gt;
  &lt;?php
          }
      }
  ?&gt;
  &lt;/select&gt;  
  </code></pre>
  
  <p>when it submits heres my code for updating the array for a specific array key:  </p>
  
  <pre><code>if(isset($_POST['quantity1'])){
      $quantity = $_POST['quantity1']; 
  
      $cart= $_SESSION['cart'];
      foreach($cart as  &amp;$value)
      {
          $value['quantity']= $quantity;
      }
  }
  </code></pre>