我从'$ _ 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;
}
print_r之后$ obj:
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
)
)
)
然而,没有任何反应......任何人都可以帮忙吗?
答案 0 :(得分:3)
在这里,在您的代码中,您只是取消设置$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]);
}
}
上面的代码完全删除了索引。