【问题标题】:How to check if multiple key equal a certain value如何检查多个键是否等于某个值
【发布时间】:2015-02-28 20:59:10
【问题描述】:

我有这个代码

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

在这个单一的条件下

if ($key == "product_id" && $value == $productid) { }

【问题讨论】:

  • 您希望 $key 同时是“size”和“product_id”还是应该是“size”或“product_id”
  • 我希望 $key "size" 和 "product_id" 都等于某个值

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


【解决方案1】:

像这样:

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;

【讨论】:

    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-17
      • 2021-06-19
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多