I have an array of products. I would like to put one more product to the array if it doesnt already exist. And get the product inside of the array and increase the item count by 1 if it is already in the array. I have the following code. It doesn`t seem to work. BTW i am new to php, sorry if this is a simple question.
if(!in_array($product, $sc->products)){
array_push($sc->products,$product);
}else{
$key = array_search($product, $sc->products);
$sc->products[$key]->productCounter++;
}
My shoppingCart class
class shoppingChart extends Model
{
public $products;
function __construct() {
$this->products = Array();
}
public function ItemCount(){
return count($this->products);
}
}
And My Product class
class Product extends Model
{
public $productCounter=0;
public function image(){
return $this->hasOne('App\Image');
}
}
$sc->productsis array of objects I suppose.in_arraywill always be false.