【问题标题】:PHP on find the array with two keys equal to two values ​in the array and remove it from the arrayPHP on 在数组中找到两个键等于两个值的数组并将其从数组中删除
【发布时间】:2020-12-04 05:28:27
【问题描述】:

我正在开发一个购物车。我将篮子放在一个会话中。我可以将产品添加到购物车。但我不知道如何从购物篮会话中删除产品线。

会话结构;会话包含一个数组和其中的产品数组。我想用两个键访问我想删除的数组。

//Create basket session
$_SESSION['basket'] = [];

//Add a Product
$productArray = ['prodID' => '1', 'quantity' => '3'];
$_SESSION['basket'][] = $productArray;

//Post method comes with two variables (keys).
$prodID = $_POST['prodID'];
$quantity = $_POST['quantity'];

//How can I remove the array with keys equal to $prodID and $quantity within the session ?



【问题讨论】:

  • 您可以将产品 id 设置为购物篮数组中的键:$_SESSON['basket'][1337] = ['prodID' => 1337, 'quantity' => 3]。然后,您始终可以通过执行以下操作访问该特定行(因为它们是在产品 ID 上设置的):$_SESSION['basket'][$prodID]
  • 我认为这是一个有吸引力的解决方案。谢谢。

标签: php arrays session


【解决方案1】:

首先,由于您的 prodID 在会话中应该是唯一的,您可以将其设置为 basket

//Create basket session
$_SESSION['basket'] = [];

//Add a Product
$productArray = ['prodID' => '1', 'quantity' => '3'];
$_SESSION['basket'][$productArray['prodID']] = $productArray;

//Post method comes with two variables (keys).
$prodID = $_POST['prodID'];
$quantity = $_POST['quantity'];

//How can I remove the array with keys equal to $prodID and $quantity within the session ?
//Now you only need to update / replace the quantity here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2011-03-04
    相关资源
    最近更新 更多