【发布时间】:2014-10-08 18:36:03
【问题描述】:
我正在使用会话变量构建购物车。我可以像这样将数组推送到会话数组:
//initialize session cart array
$_SESSION['cart'] = array();
//store the stuff in an array
$items = array($item, $qty);
//add the array to the cart
array_push($_SESSION['cart'], $items);
到目前为止,一切都很好。问题在于从购物车中删除物品。当我尝试使用它时,我得到一个数组到字符串的转换错误。
//remove an array from the cart
$_SESSION['cart'] = array_diff($_SESSION['cart'], $items);
为了澄清,这里的问题是为什么上面的语句创建数组到字符串的转换错误?
【问题讨论】:
-
任何理由使用array_push 超过
$_SESSION['cart'][]= $items; -
没那么快——根本不是重复的。在发布之前检查您的信息。
-
使用项目 id 作为键并取消设置
-
" 注意:如果你使用 array_push() 向数组添加一个元素,最好使用 $array[] = 因为这样就没有调用函数的开销。"
标签: php arrays session array-push