在PHP中,我知道没有官方的方法可以删除一旦放入数组中的项目。但是,肯定有一种“最佳方法”解决我的问题。我相信这可能在
基本上,我有一个购物车对象,它将项目存储在哈希表中。想象一下,您每次只能购买一个商品。
我做了
array_filter
函数中实现。基本上,我有一个购物车对象,它将项目存储在哈希表中。想象一下,您每次只能购买一个商品。
我做了
add_item(1);
add_item(2);
remove_item(1);
get_count()
仍然返回 2。
var $items;
function add_item($id) {
$this->items[$id] = new myitem($id);
}
function remove_item($id) {
if ($this->items[$id]) {
$this->items[$id] = false;
return true;
} else {
return false;
}
}
function get_count() {
return count($this->items);
}
人们认为在get_count中使用什么方法最好?我无法想出一种简单的使用array_filter而不返回false值的最佳方法(不需要编写单独的回调函数)。
谢谢:)