如何检查多个键是否等于某个值

时间:2015-02-28 20:59:10

标签: php multidimensional-array multiple-conditions array-splice

我有这段代码

if (isset ($_POST['id'])) {
        $productid = $_POST['id'];
        $size = $_POST['size'];
        $wasfound = false;
        $i = 0;
        if (!isset ($_SESSION['cart']) || count($_SESSION['cart']) < 1) {
            $_SESSION['cart'] = array (0 => array ("product_id" => $productid, "size" => $size, "quantity" => 1));
        }
        else {
            foreach ($_SESSION['cart'] as $eachitem) {
                $i++;
                while (list ($key, $value) = each ($eachitem)) {
                    if ($key == "product_id" && $value == $productid) {
                        array_splice ($_SESSION['cart'], $i-1, 1, array (array ("product_id" => $productid, "quantity" => $eachitem['quantity'] + 1)));
                        $wasfound = true;
                    }
                }
            }
            if ($wasfound == false) {
                array_push ($_SESSION['cart'], array ("product_id" => $productid, "size" => $size, "quantity" => 1));
            }
        }
        header ("location: cart.php");
        exit;
    }

如何添加1个条件

$ key ==“size”&amp;&amp; $ value == $ size

在这个单一条件下

if($ key ==“product_id”&amp;&amp; $ value == $ productid){ }

2 个答案:

答案 0 :(得分:0)

像这样:

if (isset ($_POST['id'])) {
        $productid = $_POST['id'];
        $size = $_POST['size'];
        $wasfound = false;
        $i = 0;
        if (!isset ($_SESSION['cart']) || count($_SESSION['cart']) < 1) {
            $_SESSION['cart'] = array (0 => array ("product_id" => $productid, "size" => $size, "quantity" => 1));
        }
        else {
            foreach ($_SESSION['cart'] as $eachitem) {
                $i++;
                while (list ($key, $value) = each ($eachitem)) {
                    if ($key == "product_id" && $value == $productid) {
                        array_splice ($_SESSION['cart'], $i-1, 1, array (array ("product_id" => $productid, "quantity" => $eachitem['quantity'] + 1)));
                        $wasfound = true;
                    }
                    else if ($key == "size" && $value == $size) {
                        //do something
                    }
                }
            }
            if ($wasfound == false) {
                array_push ($_SESSION['cart'], array ("product_id" => $productid, "size" => $size, "quantity" => 1));
            }
        }
        header ("location: cart.php");
        exit;

答案 1 :(得分:0)

如果你想做2个分离的东西,可以使用第一个,如果你想要发生同样的事情,可以使用第二个,如果其中一个条件是正确的。

while (list ($key, $value) = each ($eachitem)) {
    if ($key == "product_id" && $value == $productid) {
        array_splice ($_SESSION['cart'], $i-1, 1, array (array ("product_id" => $productid, "quantity" => $eachitem['quantity'] + 1)));
        $wasfound = true;
    } elseif ($key == "size" && $value == $size) {
        // DO SOMETHING
    }
}

while (list ($key, $value) = each ($eachitem)) {
    if (($key == "product_id" && $value == $productid) || ($key == "size" && $value == $size)) {
        array_splice ($_SESSION['cart'], $i-1, 1, array (array ("product_id" => $productid, "quantity" => $eachitem['quantity'] + 1)));
        $wasfound = true;
    }
}