2

I want to clear all elements from an array object (which can be a standard PHP array, an ArrayObject or basically any other object that implements the basic array interfaces such as Itertable, ArrayAccess, Countable etc.). However, I do not want to reinstate the object, so I somehow have to unset all the individual elements, instead of creating a new object of the same type. Is there an easy way to do this?

2
  • 4
    Why don't you want to reinstate an object/array? Commented Feb 1, 2011 at 12:36
  • Because the object is a function parameter, and I do not necessarily have knowledge of where it comes from or how to instantiate it. Commented Feb 1, 2011 at 12:56

7 Answers 7

5
foreach ($array as $key => $element) {
    unset($array[$key]);
}

This requires both Traversable and ArrayAccess, Countable is not required. Or obviously just a normal array.

Sign up to request clarification or add additional context in comments.

2 Comments

Using that gives me the following error: ArrayIterator::next(): Array was modified outside object and internal position is no longer valid
@mrjames: Hm, seems like arrayiterator is pretty strict. On arrayobject I would simply call exchangeArray, but arrayiterator doesn't have something like this.
3

I'm not entirely sure why you need to do it this way, but in answer to your question, you should be able to simply use the array_splice function to remove all of the objects from your array;

$my_array = array('A', 'B', 'C');
array_splice($my_array, 0);

I've never used array_splice to remove all objects from an array, but I assume it works in the same manner.

1 Comment

@nikic Ahh, my bad. Upvote given to your answer as it better matches the question criteria!
1

I am having trouble deciphering what the question is really asking for. Replacing the array/iterator with an empty iterator (an EmptyIterator, or another iterator with no values?) might suffice.

$array = new EmptyIterator;

Comments

0

Try

$my_array = array('A', 'B', 'C');
unset($my_array);

Comments

0

What about a simple unset?

unset($array);

Comments

0

In case you remove the last record from array than the next foreach loop can fail (internaly calling $this->next() )

So it helped me to test the validy and break in this case the next loop.

deleteOffset = true;

foreach ($iterator as $key => $value)
    {
            if ($deleteOffset && $iterator->offsetExists($key) ) 
            {
                 $iterator->offsetUnset($key);                                       
            }                

        //if remove last record than the foreach ( $this->next() ) fails so 
        //we have to break in this case the next ->next call         

        if (!$iterator->valid()) break;

    }

Comments

-1

The best place PHP Manual

http://php.net/manual/en/function.unset.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.