代码之家  ›  专栏  ›  技术社区  ›  adam

PHP-使用数组过滤器从哈希表(数组)中删除项

  •  3
  • adam  · 技术社区  · 15 年前

    array_filter 作用

    本质上,我有一个购物车对象,它将项目存储在哈希表中。想象一下,你一次只能买一件物品。

    我愿意

    add_item(1);
    add_item(2);
    remove_item(1);
    

    get_count()

    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的最佳方法,因为它不返回假值(不编写单独的回调)。

    3 回复  |  直到 15 年前
        1
  •  8
  •   Peter Bailey    15 年前

    没有官方途径?当然有! Unset !

    <?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();
    

    还有,这是PHP4吗?因为如果没有,你应该研究一些SPL的东西,比如 ArrayObject 或者至少是 Countable ArrayAccess 接口。

    下面是一个直接使用接口的版本

    <?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 );
    
        2
  •  5
  •   Mario    15 年前
    if ($this->items[$id]) {
    

    可能会返回警告,应使用array\u key\u exists或isset。

    在删除项目时,unset()似乎比赋值false更干净(也可以解决计数问题)。

        3
  •  3
  •   nickf    15 年前

    +彼得和马里奥的答案:使用 unset() 是从数组中删除项的正确方法。

    我只是想留个条子解释一下为什么你的方法不起作用。使用 count() false ,虽然你可以想象它的意思是“什么都没有”,但它实际上仍然是一个值。通常,如果您希望实际指定“无值”,则使用 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