note 56367 modified in function.arrayobject-offsetunset by helly
| From: | helly@php.net | Date: | Tue, 01 Nov 2005 21:37:33 +0000 |
| Subject: | note 56367 modified in function.arrayobject-offsetunset by helly | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97945@lists.php.net to get a copy of this message | ||
Removes an index from the array object, reducing the number of elements by one and condensing the
list.
<?php
$ao = new ArrayObject(array(1, 2, 3));
foreach($ao->getIterator() as $item) {
if ($item == 2) {
$ao->offsetUnset($ao->getIterator()->current());
}
}
echo $ao->count(); // Prints 2
?>
---
Note from the extension author:
Try this:
<?php
$ao = new ArrayObject(array(1, 2, 3));
foreach($ao as $key=>$item) { // getIterator() called automatically
if ($item == 2) {
$ao->offsetUnset($key);
}
}
echo $ao->count(); // Prints 2
?>
--was--
Removes an index from the array object, reducing the number of elements by one and condensing the
list.
<?php
$ao = new ArrayObject(array(1, 2, 3));
foreach($ao->getIterator() as $item) {
if ($item == 2) {
$ao->offsetUnset($ao->getIterator()->current());
}
}
echo $ao->count(); // Prints 2
?>
http://php.net/manual/en/function.arrayobject-offsetunset.php