PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

CachingIterator::hasNext> <ArrayObject::offsetSet
Last updated: Sun, 25 Nov 2007

view this page in

ArrayObject::offsetUnset

(PHP 5)

ArrayObject::offsetUnset — Unsets the value at the specified $index

Description

void ArrayObject::offsetUnset ( mixed $index )
Warning

Táto funkcia nie je momentálne zdokumentovaná; je dostupný len zoznam argumentov.



CachingIterator::hasNext> <ArrayObject::offsetSet
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
ArrayObject::offsetUnset
pvenakis at efront dot gr
10-Jan-2008 12:57
When traversing recursively nested arrays using an RecursiveIteratorIterator, you cannot offsetUnset() or offsetSet() sub-array values, unless they are *all* declared as ArrayObject.
oalexandrino at yahoo dot com dot br
28-May-2007 11:11
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) ;

?>
primaryspace at hotmail dot com
31-Aug-2005 12:06
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

?>

CachingIterator::hasNext> <ArrayObject::offsetSet
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites