getConnection(); $pid = trim($_POST['pid']); $price = trim($_POST['price']); $qty = trim($_POST['qty']); $exists = false; foreach($_SESSION['CART'] as $k => $item) { if($item['pid'] == $pid) { $_SESSION['CART'][$k]['qty'] += intval($qty); $exists = true; } } if(!$exists) { $_SESSION['CART'][] = array("pid" => intval($pid), "price" => intval($price), "qty" => intval($qty)); } //print_r($_SESSION['CART']); http_response_code(200); echo json_encode( array( "status" => 200, "cart" => array_values($_SESSION['CART']) )); ?>