0

我正在做一个电子商务系统。这就是填充数组的样子: [0] => Array ( [0] => 1 (this is the id) [1] => 1 ((this is the quantity) [2] => abcd (this some一种 txt 框)[3] => 10(这是价格))

[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']));
        }
4

1 回答 1

0

Try:

 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']));
    }
于 2012-11-19T14:28:10.760 回答