搜索数组,如果找到

时间:2017-03-15 00:27:27

标签: php arrays

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

非常感谢;)

2 个答案:

答案 0 :(得分:1)

这是我的实施:

$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);

答案 1 :(得分:0)

我不确定你是否有能力这样做,但如果你这样做,你可以尝试以下方法。按产品编号键入数组,并使用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