title: Array item could not be found in php shopping cart
body: <p>I am struggling with a weird situation here with arrays in PHP. I am trying to create a simple cart using a session variable.</p>
  
  <p>Problem:</p>
  
  <p>When the cart is empty, the program creates a new product with the <code>itemid</code> and <code>qty</code> variables when a product is added to cart (as required). It also does same when other new products are added. </p>
  
  <p>It also is able to update the quantity of products if added again (as required). </p>
  
  <p>But the problem here is it never finds the 1st product I add, so whenever I add that product again, it stacks the product every time and does not update the quantity of the product. Whereas for other products other than 1st, it acts as expected.</p>
  
  <p>eg. when product A is added to empty cart, it adds <code>itemid</code> as A and qty = 1. When the product A is added <strong>again</strong> to an empty cart, it adds the <code>itemid</code> as A and <code>qty=1</code> <strong>again</strong> (does not do qty=2). If I add B, C or others repeatedly it updates their <code>qty</code> as required</p>
  
  <pre><code>&lt;?php
  public function addinTable($id){
        $this-&gt;loadModel('Carts');
  
      /////////inserting into the cart table//////////7
        $item = $this-&gt;Products-&gt;get($id);
        $session = $this-&gt;request-&gt;session();
  
        $allProducts = $session-&gt;read('Cart');
  
      if(null!=$allProducts){
          echo "&lt;br&gt;if(allProducts is NOT EMPTY)&lt;br&gt;";
           if(array_search($id,array_column($allProducts, 'itemid'))){
              //if the id is already in list
                   echo "&lt;br&gt;&lt;b&gt;ITEM Is IN the list already&lt;/b&gt;";
              $key = array_search($id,array_column($allProducts, 'itemid'));
                  echo "&lt;br&gt; key is ", $key;
              $newqty = debug($allProducts[$key]['qty']);
                  echo "&lt;br&gt; new qty +1 = ".$newqty+=1;
                  debug($allProducts[$key]['qty']++);
              $session-&gt;write('Cart',$allProducts);
                 debug( $session-&gt;read('Cart'));
          }
          else{
            echo"&lt;br&gt;&lt;b&gt;The id is not found but cart is not empty&lt;/b&gt;";
              $allProducts[] = array('itemid'=&gt;$id,
                                      'qty' =&gt; 1
                                    );
              debug( $session-&gt;read('Cart'));
          }
  
      }
       else{///////////if cart is empty at first
           echo"&lt;br&gt;&lt;b&gt;The  cart is  empty&lt;/b&gt;";
         $allProducts[] = array('itemid'=&gt;$id,'qty' =&gt; 1);
  
            debug($allProducts[0]);
            debug($allProducts);
            debug($allProducts[0]['itemid']);
  
          //  if(array_search($id,array_column($allProducts, 'itemid'))==true){echo "hello";}
            $session-&gt;write('Cart',$allProducts);
                   debug($session-&gt;read('Cart'));
                       }
              $session-&gt;write('Cart',$allProducts);//save the item
  
    }
  ?&gt;
  </code></pre>