PHP-使用数组过滤器从哈希表(数组)中删除项
在PHP中,我知道一旦将项目放入数组中,就没有正式的方法来删除它们。但必须有一个“最佳方法”来解决我的问题。我相信这可能存在于PHP-使用数组过滤器从哈希表(数组)中删除项,php,arrays,array-filter,Php,Arrays,Array Filter,在PHP中,我知道一旦将项目放入数组中,就没有正式的方法来删除它们。但必须有一个“最佳方法”来解决我的问题。我相信这可能存在于array\u filter函数中 本质上,我有一个购物车对象,它将项目存储在哈希表中。想象一下,你一次只能买一件物品 我知道 get\u count()仍然返回2 var $items; function add_item($id) { $this->items[$id] = new myitem($id); } function remove_it
array\u filter
函数中
本质上,我有一个购物车对象,它将项目存储在哈希表中。想象一下,你一次只能买一件物品
我知道
get\u 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);
}
人们认为什么是最好的计算方法?我想不出使用array_filter的最佳方法,因为它不返回假值(不编写单独的回调)
谢谢:)
可能会返回警告,应使用array\u key\u exists或isset
在删除项目时,unset()似乎比赋值false更干净(也可以解决计数问题)。没有官方方法?当然有
彼得和马里奥的答案都是:使用
unset()
是从数组中删除项的正确方法
我只是想留个条子解释一下为什么你的方法不起作用。使用count()
将计算数组中的值数false
,虽然你可以想象它的意思是“什么都没有”,但它实际上仍然是一个值。通常,如果您希望实际指定“无值”,请使用null
您还可以通过将数组元素设置为null
,来取消设置数组元素,不过请查看下面的代码以查看差异
$x = array("a", "b", "c");
var_dump(isset($x[0])); // bool(true)
var_dump(isset($x[1])); // bool(true)
var_dump(isset($x[2])); // bool(true)
echo count($x); // 3
$x[2] = null;
var_dump(isset($x[2])); // bool(false) -- the value is not set any longer
echo count($x); // 3 -- but it doesn't remove it from the array
unset($x[1]);
var_dump(isset($x[1])); // bool(false)
echo count($x); // 2
摆脱isset条件;)哦,我想不需要了。我没有测试代码,但我想它毕竟不会对unset()发出警告。
if ($this->items[$id]) {
<?php
class foo
{
var $items = array();
function add_item($id) {
$this->items[$id] = new myitem($id);
}
function remove_item($id)
{
unset( $this->items[$id] );
}
function get_count() {
return count($this->items);
}
}
class myitem
{
function myitem( $id )
{
// nothing
}
}
$f = new foo();
$f->add_item( 1 );
$f->add_item( 2 );
$f->remove_item( 1 );
echo $f->get_count();
<?php
class foo implements ArrayAccess, Countable
{
protected $items = array();
public function offsetExists( $offset )
{
return isset( $this->items );
}
public function offsetGet( $offset )
{
return $this->items[$offset];
}
public function offsetSet( $offset, $value )
{
$this->items[$offset] = $value;
}
public function offsetUnset( $offset )
{
unset( $this->items[$offset] );
}
public function count()
{
return count( $this->items );
}
public function addItem( $id )
{
$this[$id] = new myitem( $id );
}
}
class myitem
{
public function __construct( $id )
{
// nothing
}
}
$f = new foo();
$f->addItem( 1 );
$f->addItem( 2 );
unset( $f[1] );
echo count( $f );
<?php
class foo extends ArrayObject
{
public function addItem( $id )
{
$this[$id] = new myitem( $id );
}
}
class myitem
{
public function __construct( $id )
{
// nothing
}
}
$f = new foo();
$f->addItem( 1 );
$f->addItem( 2 );
unset( $f[1] );
echo count( $f );
$x = array("a", "b", "c");
var_dump(isset($x[0])); // bool(true)
var_dump(isset($x[1])); // bool(true)
var_dump(isset($x[2])); // bool(true)
echo count($x); // 3
$x[2] = null;
var_dump(isset($x[2])); // bool(false) -- the value is not set any longer
echo count($x); // 3 -- but it doesn't remove it from the array
unset($x[1]);
var_dump(isset($x[1])); // bool(false)
echo count($x); // 2