[1] => Array
(
[0] => 3
[1] => 1
[2] => efg
[3] => 50
)
[2] => Array
(
[0] => 1
[1] => 1
[2] => bla bla bla
[3] => 10
)
现在我需要检查插入时是否存在id,如果它只是和数量。 这是代码:
if (!empty($_SESSION["cart"])) {
$array = $_SESSION['cart'];
$item = array($_POST['id'], $_POST["quantity"], $_POST['txt'], $_POST['price']);
array_push($array, $item);
$_SESSION["cart"] = $array;
} else {
$_SESSION["cart"] = array(array($_POST['id'], $_POST["quantity"], $_POST['txt'], $_POST['price']));
}
答案 0 :(得分:0)
尝试:
if (!empty($_SESSION["cart"])) {
$item =array();
$array = $_SESSION['cart'];
if(!empty($_POST['id'])
$item = array($_POST['id'], $_POST["quantity"], $_POST['txt'], $_POST['price']);
array_push($array, $item);
$_SESSION["cart"] = $array;
} else {
$_SESSION["cart"] = array(array($_POST['id'], $_POST["quantity"], $_POST['txt'], $_POST['price']));
}