Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查多个键是否等于某个值_Php_Multidimensional Array_Multiple Conditions_Array Splice - Fatal编程技术网

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

Php 如何检查多个键是否等于某个值,php,multidimensional-array,multiple-conditions,array-splice,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'

我有这个密码

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&&$value==size

在这个条件下

如果$key==product\u id&&$value==productid{ }像这样:

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;

如果你想做两件不同的事情,可以使用第一种,如果其中一个条件正确,如果你想做同样的事情,可以使用第二种

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;
    }
}

您希望$key同时是size和product_id,还是应该是size或product_id?我希望$key size和product_id都等于某个值