搜索阵列,如果找到


Array
(
    [0] => Array
        (
            [product_number] => 1000000002
            [quantity] => 1
            [size] => L
        )
    [1] => Array
        (
            [product_number] => 1000000002
            [quantity] => 1
            [size] => XS
        )
    [2] => Array
        (
            [product_number] => 1000000002
            [quantity] => 1
            [size] => M
        )
)

你好,

我需要一些帮助。我正在创建一个商店系统。我正在尝试将产品添加到篮子/购物车中。

我需要检查产品号是否在数组中,如果是?那大小是合适的吗? - 如果是,然后更新正确的一个..

如果大小不是正确的添加新的。

如果产品编号不在数组中。添加新的

我已经尝试了in_array,foreach和一些东西。你们中的任何人都可以帮我吗?

它在php

非常感谢;)

这是我的实现:

$ShoppingBasket = Array
(
     Array
        (
            "product_number" => 1000000002,
            "quantity" => 1,
            "size" => "L"
        ),
    Array
        (
            "product_number" => 1000000002,
            "quantity" => 1,
            "size" => "XS"
        ),
    Array
        (
            "product_number" => 1000000002,
            "quantity" => 1,
            "size" => "M"
        )
);
function addItem(&$ShoppingBasket, $PN, $size) 
{ //use references with & to modify the array directly 
    // loop through each element of your array (mind this is an array of hashes.
    foreach ($ShoppingBasket as &$item) //reference again so it modifies the item direclty in $ShoppingBasket.
    {
        // compare if the PN and size are the same 
        if ($item["product_number"] == $PN && strcmp($item["size"],$size) == 0)
        {
                $item["quantity"] ++; //increase the quantity count
                return; //end the function if we found the item.
        }
    }
    // this acts as a else close, it only executes if we haven't found anything already.
    // push a new hash in $ShoppingBasket where all the field are specified.
    array_push($ShoppingBasket, array("product_number" => $PN,"quantity" => 1,"size" => $size));
    return;
}
function removeItem(&$ShoppingBasket, $PN, $size) 
{ //use references with & to modify the array directly 
    for($i = 0 ; $i < count($ShoppingBasket) ; $i++) 
    {//need to interate with a counter to have the position in the array
        if ($ShoppingBasket[$i]["product_number"] == $PN && strcmp($ShoppingBasket[$i]["size"],$size) == 0)
        {
               array_splice($ShoppingBasket, $i, 1);
               //splice removes 1 element from position $i
               return TRUE; //we have removed something
        }
    }
    return FALSE; // we have not removed the item
}
//little test of the function
addItem($ShoppingBasket,1000000002, "M");
addItem($ShoppingBasket,1000000002, "M");
addItem($ShoppingBasket,1000000002, "XXL");
addItem($ShoppingBasket,1000000232, "M");
print_r($ShoppingBasket);
removeItem($ShoppingBasket,1000000232, "M");
removeItem($ShoppingBasket,1000000002, "M");
removeItem($ShoppingBasket,1000000002, "XXL");
print_r($ShoppingBasket);

我不确定您是否有能力执行此操作,但是如果您这样做,则可以尝试以下内容。按您的产品编号键并使用array_key_exists()函数来确定是否存在该密钥。

if( array_key_exists( $product_number, $product_array ) )
{
    //
    // The product number was found in the array of products
    //
}
else
{
    //
    // The product number was not found in the array of products
    //
}

参考:http://php.net/manual/en/function.array-key-exists.php

相关内容

最新更新