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

  • SEARCH
  • Home
  • 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
  • 1 View
  • 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

OK so I now know there is a way to put all my php
I need to know if there's a way in PHP to echo a string
I want to know if there is a way to display an external php
Is there a way to know where is set a given PHP config property?
Is there a way to get PHP-like print_r(object) funcionality in iPython? I know I
Does anyone know if there is a php library, or if there isnt, have
Possible Duplicate: Convert one date format into another in PHP I know there are
I'd like to know if there is somethings like Javadoc but for PHP ,
I know there are MD5 and SHA1 hashing functions in PHP, but are there
I know there are natsort() and natcasesort() functions in php to sort array elements

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.