Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 198771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:57:58+00:00 2026-05-11T16:57:58+00:00

In PHP, I know there is no official way to delete items once they

  • 0

In PHP, I know there is no official way to delete items once they have been put into an array. But there must be a “best-method” solution to my problem. I believe this may lie in the array_filter function.

Essentially, I have a shopping cart object that stores items in a hashtable. Imagine you can only ever buy one of any item 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 can’t figure out the best way to use array_filter that simply doesn’t return false values (without writing a seperate callback).

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:57:58+00:00Added an answer on May 11, 2026 at 4:57 pm

    No official way? Sure there is! 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();
    

    Also, is this PHP4? Because if not, you should look into some of the SPL stuff like ArrayObject or at least the the Countable and ArrayAccess interfaces.

    EDIT

    Here’s a version using the interfaces directly

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

    And here’s a version as an implementation of 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 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I know there have been a number of similar posts, but I think
I know that PHP doesn't yet have native Enumerations. But I have become accustomed
What's the best way to print from MySQL using PHP when you know there's
I'd like to know if there is somethings like Javadoc but for PHP ,
I know PHP scripts don't actually compile until they are run. However, say I
I know that this is a simple question for PHP guys but I don't
I know how to disable WSDL-cache in PHP, but what about force a re-caching
I don't know the official terms for what this is called, but would greatly
I know a bunch of scripting languages, (python, ruby, lua, php) but I don't
I know there is a way to programmatically invoice, ship, and set state on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.