Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Standard PHP Library (SPL) Functions : ArrayObject::offsetUnset

ArrayObject::offsetUnset

Unsets the value at the specified $index ()
void ArrayObject::offsetUnset ( mixed index )

Warning:

This function is currently not documented; only the argument list is available.

Code Examples / Notes » arrayobject_offsetunset

primaryspace

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


oalexandrino

Be careful when you are working with collections. This method works with the reference of an array instead of its retrieved value.
So, you can do a mistake.
In order to understand have a look at code as follow:
<?php
class Employee
{
   public function __construct()
   {
   }
}
class Company
{
private $arrEmployee;

   public function __construct()
   {
   }
   
   public function AddEmployee(Employee $oEmployee)
   {
    $this->arrEmployee[] = $oEmployee;
   
   }
   
   public function getEmployeeList()
   {
    return $this->arrEmployee;
   
   }

}
?>
<?php
// first, creates the Company object
$oCompany = new Company();
// second, add 10 elements in
foreach( range(0, 9) as $index )
{
$oCompany->AddEmployee( new Employee() );
}
// get them
$arrEmployee = $oCompany->getEmployeeList();
// creates an ArrayObject from "$arrEmployee"
$arrayobject = new ArrayObject($arrEmployee);
// unsets its firt five elements
foreach( range(0, 4) as $index )
{
$arrayobject->offsetUnset($index);
}
// get them again
$arrEmployee = $oCompany->getEmployeeList();
// it shows just 5 elements, they were removed as reference via "offsetUnset" method
print_r($arrEmployee) ;
?>


Change Language


Follow Navioo On Twitter
ArrayIterator::current
ArrayIterator::key
ArrayIterator::next
ArrayIterator::rewind
ArrayIterator::seek
ArrayIterator::valid
ArrayObject::append
ArrayObject::__construct
ArrayObject::count
ArrayObject::getIterator
ArrayObject::offsetExists
ArrayObject::offsetGet
ArrayObject::offsetSet
ArrayObject::offsetUnset
CachingIterator::hasNext
CachingIterator::next
CachingIterator::rewind
CachingIterator::__toString
CachingIterator::valid
CachingRecursiveIterator::getChildren
CachingRecursiveIterator::hasChildren
DirectoryIterator::__construct
DirectoryIterator::current
DirectoryIterator::getATime
DirectoryIterator::getCTime
DirectoryIterator::getFilename
DirectoryIterator::getGroup
DirectoryIterator::getInode
DirectoryIterator::getMTime
DirectoryIterator::getOwner
DirectoryIterator::getPath
DirectoryIterator::getPathname
DirectoryIterator::getPerms
DirectoryIterator::getSize
DirectoryIterator::getType
DirectoryIterator::isDir
DirectoryIterator::isDot
DirectoryIterator::isExecutable
DirectoryIterator::isFile
DirectoryIterator::isLink
DirectoryIterator::isReadable
DirectoryIterator::isWritable
DirectoryIterator::key
DirectoryIterator::next
DirectoryIterator::rewind
DirectoryIterator::valid
FilterIterator::current
FilterIterator::getInnerIterator
FilterIterator::key
FilterIterator::next
FilterIterator::rewind
FilterIterator::valid
LimitIterator::getPosition
LimitIterator::next
LimitIterator::rewind
LimitIterator::seek
LimitIterator::valid
ParentIterator::getChildren
ParentIterator::hasChildren
ParentIterator::next
ParentIterator::rewind
RecursiveDirectoryIterator::getChildren
RecursiveDirectoryIterator::hasChildren
RecursiveDirectoryIterator::key
RecursiveDirectoryIterator::next
RecursiveDirectoryIterator::rewind
RecursiveIteratorIterator::current
RecursiveIteratorIterator::getDepth
RecursiveIteratorIterator::getSubIterator
RecursiveIteratorIterator::key
RecursiveIteratorIterator::next
RecursiveIteratorIterator::rewind
RecursiveIteratorIterator::valid
SimpleXMLIterator::current
SimpleXMLIterator::getChildren
SimpleXMLIterator::hasChildren
SimpleXMLIterator::key
SimpleXMLIterator::next
SimpleXMLIterator::rewind
SimpleXMLIterator::valid
class_implements
class_parents
iterator_count
iterator_to_array
spl_autoload_call
spl_autoload_extensions
spl_autoload_functions
spl_autoload_register
spl_autoload_unregister
spl_autoload
spl_classes
spl_object_hash
eXTReMe Tracker