【问题标题】:Search array, and if found搜索数组,如果找到
【发布时间】:2017-03-15 18:56:13
【问题描述】:
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 中

非常感谢;)

【问题讨论】:

  • 告诉我们什么语言并发布您尝试的代码

标签: php arrays


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

【讨论】:

  • 我不知道该怎么感谢你!我现在用了.. 6 个小时.. 我想.. 试图解决这个问题.. 我可以请你为我解释一下代码吗? - 所以我学到了一些新东西?
  • 我快哭了! :D
  • 我已经对代码进行了注释。如果您仍然难以理解,我猜您对 PHP 有点陌生。不用担心你会通过练习得到它。
  • 酷 m8,我明白了,我现在做错了什么:) .. 非常感谢!为什么我不能将 mysqli_query 放入函数中?更新数组时,我也想在 db 中更新我的表...
  • 是否也可以制作 removeItme ? :)
【解决方案2】:

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

【讨论】:

  • 那么您可以执行另一项检查,因为这样您就可以轻松访问您的产品 - if( $product_array[ $product_number ]['size'] == '' ) {}
猜你喜欢
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2016-04-12
  • 2014-09-13
  • 1970-01-01
  • 2021-02-26
  • 2017-08-12
  • 2010-09-26
相关资源
最近更新 更多