0

I have an session cookie holding a multidimensional array called cart_array and I'm using a for each to loop through the inner arrays and a while loop get the key value pairs out.

I want to check if an item exists in the array based not only on the product id (pid) but several other variables such as color and size. Here's what I've come up with so far (but it only checks for the pid). How can I check for the other two variables?

Here are my variables

$_SESSION['cart_array'] = array(1 => array(
        "pid" => $pid,
        "quantity" => $quantity,
        "color" => $color,
        "size" => $size,
        "title" => $title,
        "product_type" => $product_type,
        "price" => $price))

Here is the code for and while loop combination:

foreach($_SESSION['cart_array'] as $each_item) {
            $index++;
            while(list($key, $value) = each($each_item)) {
                if($key == "pid" && $value == $pid) {
                    //That item is in the array
                    echo "This item is in the array";
                } else {
                    echo "This item is not in the cart";
                }
            }
        }
4

3 回答 3

0

如果我说对了,这可能会有所帮助:

$_SESSION['cart_array'] = array(1 => array(
    "pid" => $pid,
    "quantity" => $quantity,
    "color" => $color,
    "size" => $size,
    "title" => $title,
    "product_type" => $product_type,
    "price" => $price));

foreach($_SESSION['cart_array'] as $item) {
    foreach($item as $key => $value) {
        if( empty($value) ) {
            echo "This item is not in the cart";
            continue 2;
        }
    }

    echo "This item is in the cart";
}

这将检查项目的每个字段。如果您需要具有一组排除项的解决方案,或者您需要将元素与一组值进行比较 - 请在评论中告诉我。

于 2013-07-25T11:14:17.120 回答
0

你有没有尝试过:

foreach($_SESSION['cart_array'] as $item) {
    $index++;
    $pid_matches = $color_matches = $size_matches = false;
    foreach($item as $key => $value) {
        if($key == 'pid' && $value == $pid) {
            $pid_matches = true;
        }
        elseif($key == 'color' && $value == $color){
            $color_matches = true;
        }
        elseif($key == 'size' && $value == $size){
            $size_matches = true;
        }
    }
    if($pid_matches && $color_matches && $size_matches){
        echo "This item is in the array";
    }
    else {
        echo "This item is not in the cart";
    }
}
于 2013-07-25T11:04:08.467 回答
0

我会做这样的事情:

    foreach($_SESSION['cart_array'] as $each_item) {
        $index++;

        $pidTest = false;
        $colorTest = false;
        $sizeTest = false;

        while(list($key, $value) = each($each_item)) {
            if($key == "pid" && $value == $pid) {
               $pidTest = true;
            }

            if($key == "color" && $value == $color) {
               $colorTest = true;
            } 
        }

        if ($pidTest && $colorTest && sizeTest)
        {
             echo "Item is in the cart";
        }
        else
        {
             echo "Nope";
        }
    }

当然,您可以更优雅、更动态地处理这个问题,但这是您可以使用的基本逻辑。

于 2013-07-25T11:03:15.827 回答