【问题标题】:Deleteing object from $_SESSION array从 $_SESSION 数组中删除对象
【发布时间】:2016-10-03 13:15:12
【问题描述】:

我在从 '$_SESSION' 数组中删除对象时遇到问题。 我的目标是在选择一个特定产品后从阵列中删除每个产品。这是视图部分:

<?php                   
  for ($i=0; $i < count($this->products); $i++) { 
     echo "<a class='remove_from_basket' href='" .$this->baseUrl. "/shop/delete-product/id/" .$this->products[$i]->product_id. "'>Delete</a>";
  }    
?>

然后在 PHP 部分我得到这个产品 ID:

public function deleteProductAction() {    
        $productID = $this->_getParam('id', 0);
        session_start();
        $obj = $_SESSION['products'];    
        foreach ($obj as $key => $product) {
            if ($product['product_id'] == $productID) {
                unset($product);
            }
        }
        $_SESSION['products'] = $obj;    
    }

打印后的$obj_r:

Array
(
    [2] => Zend_Db_Table_Row Object
        (
            [_data:protected] => Array
                (
                    [product_id] => 26
                )

            [_cleanData:protected] => Array
                (
                    [product_id] => 26

            [_modifiedFields:protected] => Array
                (
                )

            [_table:protected] => 
            [_connected:protected] => 
            [_readOnly:protected] => 
            [_tableClass:protected] => Application_Model_DbTable_Products
            [_primary:protected] => Array
                (
                    [1] => product_id
                )

        )

    [3] => Zend_Db_Table_Row Object
        (
            [_data:protected] => Array
                (
                    [product_id] => 26
                )

            [_cleanData:protected] => Array
                (
                    [product_id] => 26
                )

            [_modifiedFields:protected] => Array
                (
                )

            [_table:protected] => 
            [_connected:protected] => 
            [_readOnly:protected] => 
            [_tableClass:protected] => Application_Model_DbTable_Products
            [_primary:protected] => Array
                (
                    [1] => product_id
                )

        )

)

但是,什么都没有发生...有人可以帮忙吗?

【问题讨论】:

    标签: php arrays object foreach


    【解决方案1】:

    在这里,在您的代码中,您只是取消设置$product,这与您的会话变量完全无关。您需要使用对象的index,并在原始对象中取消设置。

    foreach ($_SESSION['products'] as $key => $product) {
        if ($product['product_id'] == $productID) {
            unset($_SESSION['products'][$key]);
        }
    }
    

    或者只是为了你想做的$obj,你可以这样做:

    foreach ($obj as $key => $product) {
        if ($product['product_id'] == $productID) {
            unset($obj[$key]);
        }
    }
    

    上面的代码完全删除了索引。

    【讨论】:

    • 好的,但是经常使用这个,当在视图部分显示时,我在这个特定的索引中得到一个通知:注意:未定义的偏移量:0
    • 我会在 of 语句中添加一个检查,以查看您要取消设置的数据是否存在。像这样:if (isset($_SESSION['products'][$key]) &amp;&amp; $product['product_id'] == $productID) { unset($_SESSION['products'][$key]);}`
    • @Sitethief 没有 unset 悄悄地取消设置?
    • 你是对的 这可能是微不足道的,但取消设置不存在的变量没有错误。
    • @Peter var_export 不是 var_dumpprint_r。但这也有帮助。
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 2016-11-26
    • 2016-05-19
    • 2011-04-04
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多