title: php foreach with if loop through array of objects performing if condition times members in array instead of only once
body: <p><code>$_SESSION['quantity']</code> is an array of Objects. </p>
  
  <p>let's say I already added a product with <code>id = 1</code> to the array.</p>
  
  <p>Next I add same product with <code>id = 1</code> , so now <code>$sameProduct = 1</code> and is set.</p>
  
  <p><code>$q-&gt;update()</code> increments by the property 'quantity' of that object by 1.</p>
  
  <p>The code works well when there is only one item in the array, the problem starts when there are more than one item. The code runs times there items in the array instead of only once for that particular object.   </p>
  
  <pre><code>   &lt;?php 
  if(!session_start()){session_start();}
  
  require_once 'components/sql_login.php';
  require_once 'components/header.php';
  
  if(isset($_SESSION['userSession']) != ""){
      echo "&lt;script&gt; window.location.href = 'user/cart.php'; &lt;/script&gt;";
      }
  
  if(!isset($_SESSION['productIds'])){$_SESSION['productIds']=array();}
  if(!isset($_SESSION['cartProducts'])){$_SESSION['cartProducts']=array();}
  if(!isset($_SESSION['quantity'])){$_SESSION['quantity']=array();}
  
  if (isset($_GET['f'])){ 
      if(!in_array($_GET['f'],$_SESSION['productIds'])){
      $newFairy = $_GET['f'];
      array_push($_SESSION['productIds'],$newFairy);
  
          } else {$oldFairy = $_GET['f'];}
      }
  
  
      foreach($_SESSION['productIds'] as $newItem){
      $query = "SELECT * FROM products WHERE id = ?";
      if($getFairy = $sqlConnection-&gt;prepare($query)){
          $getFairy-&gt;bind_param("i",$newItem);
          $getFairy-&gt;execute();
          $result = $getFairy-&gt;get_result();
          $row    = $result-&gt;fetch_array();
          $getFairy-&gt;close();
  
              $newProduct = new AddProduct($row);
              }
  
      if (!in_array($newProduct,$_SESSION['cartProducts'])){
  
          array_push($_SESSION['cartProducts'],$newProduct);
          $quantity   = new quantityUpdate($newProduct);
          array_push($_SESSION['quantity'],$quantity);
              print_r($_SESSION['quantity']);
                  } else {
  
                          if(isset($oldFairy) != ""){
                          $founded = false;
                      foreach($_SESSION['quantity'] as $q){
  
                      if($q-&gt;id == $oldFairy &amp;&amp; $founded != true){        
                          $q-&gt;update();
                          $founded = true;
  
                      print_r($_SESSION['quantity']);
                      echo "&lt;br&gt;count: ".count($_SESSION['quantity'])."&lt;br&gt;";
  
                              }
                          }
                      }
                  }
              }
  
  
  if($_SESSION['productIds'] != ""){
  
      $c = count($_SESSION['cartProducts']);
  
  
          if($c == 0){
          $outPutProduct = "&lt;h3&gt;כרגע אין מוצרים בעגלת הקניות&lt;/h3&gt;";
  
          echo "&lt;br&gt;cartProducts: ".$c;
          echo "&lt;br&gt;productIds: ".count($_SESSION['productIds']);
          } else if ($c == 1){
          $outPutProduct = $_SESSION['cartProducts'][0]-&gt;output;
  
          echo "&lt;br&gt;cartProducts: ".$c;
          echo "&lt;br&gt;productIds: ".count($_SESSION['productIds']);
          } else {                
  
                  $outPutProduct = $_SESSION['cartProducts'][0]-&gt;output;
              for($x=1; $x&lt;count($_SESSION['cartProducts']);$x++){
                  $outPutProduct .= "&lt;hr&gt;".$_SESSION['cartProducts'][$x]-&gt;output;
  
                  }
                  echo "&lt;br&gt;cartProducts: ".$c;
                  echo "&lt;br&gt;productIds: ".count($_SESSION['productIds']);
          }
  
      } 
  
  Class quantityUpdate{
  
      var $id;
      var $quantity = 1;
  
      function __construct($obj){
          $this-&gt;id = $obj-&gt;id;
          }
  
      function update(){
          $this-&gt;quantity += 1;
          }
      }   
  
  Class AddProduct{
  
      var $image;
      var $name;
      var $selectQnty;
      var $desc;
      var $id;
      var $price;
      var $output;
  
      function __construct($fairy){
          $this-&gt;image        = $fairy['img'];
          $this-&gt;name         = $fairy['name'];
          $this-&gt;desc         = $fairy['description'];
          $this-&gt;id           = $fairy['id'];
          $this-&gt;price        = $fairy['price'];
          $this-&gt;selectQnty   = "&lt;select id='quantity".$this-&gt;id."'&gt;" 
                              ."&lt;option id='1' value='1'&gt;1&lt;/option&gt;"
                              ."&lt;option id='2' value='2'&gt;2&lt;/option&gt;"
                              ."&lt;option id='3' value='3'&gt;3&lt;/option&gt;"
                              ."&lt;option id='4' value='4'&gt;4&lt;/option&gt;"
                              ."&lt;option id='5' value='5'&gt;5&lt;/option&gt;"
                              ."&lt;option id='6' value='6'&gt;6&lt;/option&gt;"
                              ."&lt;option id='7' value='7'&gt;7&lt;/option&gt;"
                              ."&lt;option id='8' value='8'&gt;8&lt;/option&gt;"
                              ."&lt;option id='9' value='9'&gt;9&lt;/option&gt;"
                              ."&lt;option id='10' value='10'&gt;10&lt;/option&gt;"
                              ."&lt;/select&gt;"
                              ."&lt;span class='qnty'&gt;x&lt;/span&gt;";
  
          $this-&gt;output       = $this-&gt;image.$this-&gt;selectQnty
                              ."&lt;span id='name' class='name'&gt;".$this-&gt;name."&lt;/span&gt;"
                              ."&lt;span id='desc' class='desc'&gt;".$this-&gt;desc."&lt;/span&gt;"
                              ."&lt;span id='id' class='id'&gt;מק״ט: 000".$this-&gt;id."&lt;/span&gt;"
                              ."&lt;span id='price' class='price'&gt;".$this-&gt;price."₪&lt;/span&gt;"
                              ."&lt;button onclick='removeItem(".$this-&gt;id.")' class='glyphicon glyphicon-remove'&gt;&lt;/button&gt;";
  
          }   
      }
  
  ?&gt;
  </code></pre>