PHP - Remove an item from a Hash Table (array) using array_filter

In PHP, I know that there is no official way to delete elements after they are entered into an array. But for my problem there must be a solution to the "best method". I believe this may be in function array_filter.

Essentially, I have a shopping cart object that stores items in a hash table. Imagine that you can only buy one of the items at a time.

I do

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

get_count() still returns 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);
}

What do people think is the best method to use in get_count? I cannot find a better way to use array_filter, which simply does not return false values ​​(without writing a separate callback).

Thanks:)

+3
source share
3

? , ! 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 );

ArrayObject

<?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 );
+8
if ($this->items[$id]) {

, array_key_exists isset.

unset() , false ( ).

+5

+1 , : unset() - .

I just wanted to leave a note to explain why your method does not work. Usage count()will count the number of values ​​in the array. false, although you can imagine that it means “nothing,” it is still really value. Usually, if you want to indicate "no value", use null.

You can also disable the array element by setting it to null, although look at the code below to see the difference.

$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
+3
source

Source: https://habr.com/ru/post/1707289/


All Articles