【问题标题】:Trying to remove session array from array PHP试图从数组 PHP 中删除会话数组
【发布时间】:2015-04-02 10:19:00
【问题描述】:

我试图从一个多数组中删除一个 session['cart_items] 数组,但我似乎无法让删除部分工作。

我正在通过:$_POST['product] 获取 ID,但是我尝试了以下操作并有点卡住:

$temp = array_flip($_SESSION['cart_items']);    
unset($_SESSION['cart_items'][$temp[$_POST['product']]]);

我也试过了:

unset($_SESSION['cart_items'][$key]);

我的输出是:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [item_id] => 407
                [item_name] => Am I Bothered? About Homophobia
                [item_qty] => 22
            )

    )

)

帮助会很大

多个数组结果:

array(1) {
  ["cart_items"]=>
array(2) {
[1]=>
array(1) {
  [0]=>
  array(3) {
    ["item_id"]=>
    string(3) "407"
    ["item_name"]=>
    string(31) "Am I Bothered? About Homophobia"
    ["item_qty"]=>
    string(2) "50"
  }
}
[2]=>
array(1) {
  [0]=>
  array(3) {
    ["item_id"]=>
    string(4) "1131"
    ["item_name"]=>
    string(50) "10 Ways A Condom Can’t Protect You – Postcards"
    ["item_qty"]=>
    string(2) "14"
  }
}
}
}

【问题讨论】:

  • $_SESSION['cart_items'][$key] = "" ?
  • $key=array_search($_POST['product'],$_SESSION['cart_items']);
  • $key 值是多少?它是相同的数组索引吗?
  • 我的意思是用那个代替unset()
  • 是的,试图删除 item_id = $_POST['product'] 的数组

标签: php arrays session


【解决方案1】:
for(i=0;i<sizeof($_SESSION['cart_items'][0]);i++){
    if($_SESSION['cart_items'][0][i]['item_id'] == $key);
        unset($_SESSION['cart_items'][0][i]['item_id']);
}

【讨论】:

  • 这几乎可以工作 - 除了我想删除 item_id = $key 的整个数组之外,我还尝试从 unset() 中删除 ['item_id'] 但它没有用。快到了;)
  • for(i=0;i
  • 很不幸,我要在我的 cmets 中指出这个问题。看看我的cmets
  • 这适用于购物车中的单个商品 - 当有多个商品时,它无法将其从会话中删除。
  • 给我看看你的多重数组,然后我会告诉你的。
猜你喜欢
  • 2015-08-29
  • 2014-10-08
  • 2011-01-14
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多