I have an array with two dimensions. It looks like this:
Array (
[TOKEN] => c374fe5a38b49940b7794ea44185,
[0] => Array (
[ID] => 1,
[TITLE] => Product_1,
[PRICE] => 14.99,
[QTY] => 1,
)
[1] => Array (
[ID] => 2,
[TITLE] => Product_2,
[PRICE] => 14.99,
[QTY] => 1,
)
)
I want to increase the value of quantity by one if there is a product with the same ID.
My Code looks like this:
// PRODUCT DATA
$res = mysqli_query($link, "SELECT * FROM products WHERE id='$pid'") or die(mysqli_error($link));
$row = mysqli_fetch_array($res, MYSQLI_ASSOC);
// PRODUCT ARRAY - PREPARE FOR INSERT IN ARRAY
$product = array(
"ID" => $row['id'],
"TITLE" => $row['title'],
"PRICE" => $row['price'],
"QTY" => "1"
);
function array_search_recursive($needle, $haystack) {
foreach ($haystack as $value) {
if (is_array($value) && array_search_recursive($needle, $value)) return true;
else if ($value == $needle) return true;
}
return false;
}
// PRODUCT ALREADY IN CART ARRAY?
if(array_search_recursive($row['id'], $_SESSION['CART']) == TRUE){
// INCREASE QTY BY ONE
} else {
// ADD PRODUCT TO ARRAY
array_push($_SESSION['CART'], $product);
return true;
}
}
if(sh_add_cart($_GET['pid']) == TRUE){
echo "PRODUCT ADDED.";
} else {
echo "NOT ADDED.";
}