i'm a beginner in php, so apologize if you will find the question a little weird.
i'm trying to check 2 variables that i POST and store in a session array. The 2 variables are the size and the color of an item in a shopping cart.
How can I check if theses 2 variables are already existing in my session array. If it's the case i just want to increment them quantity. In fact, i think i'm just missing to write my if statement for check this case. Here's the code:
if (isset($_POST['ID']) && isset($_POST['SIZE']) && isset($_POST['COLOR'])) {
$product = $_POST['ID'];
$size = $_POST['SIZE'];
$color = $_POST['COLOR'];
$Found = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["myArray"]) || count($_SESSION["myArray"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["myArray"] = array(0 => array("productID" => $product, "quantity" => 1, "size"=> $size, "color" => $color));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["myArray"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item))
{
if (($key == "productID" && $value == $product))//<- IT'S PROBABLY WHERE I SHOULD CHECK THEM BUT HOW????
{
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["myArray"], $i-1, 1, array(array("productID" => $product, "quantity" => $each_item['quantity'] + 1, "size"=> $size, "color" => $color)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($Found == false) {
array_push($_SESSION["myArray"], array("productID" => $product, "quantity" => 1, "size"=> $size, "color" => $color));
}
}
}
Thanks in advance for your helps guys.