PHP Doku:: Unsets the value at the specified index - arrayobject.offsetunset.html
Bisherige Suchanfragen: (1) anzeigen

Verlauf / Chronik / History: (7) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)Verschiedene Klassen und InterfacesThe ArrayObject classArrayObject::offsetUnset

Ein Service von Reinhard Neidl - Webprogrammierung.

The ArrayObject class

<<ArrayObject::offsetSet

ArrayObject::serialize>>

ArrayObject::offsetUnset

(PHP 5 >= 5.0.0)

ArrayObject::offsetUnsetUnsets the value at the specified index

Beschreibung

void ArrayObject::offsetUnset ( mixed $index )

Unsets the value at the specified index.

Parameter-Liste

index

The index being unset.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Beispiele

Beispiel #1 ArrayObject::offsetUnset() example

<?php
$arrayobj 
= new ArrayObject(array(0=>'zero',2=>'two'));
$arrayobj->offsetUnset(2);
var_dump($arrayobj);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

object(ArrayObject)#1 (1) {
  [0]=>
  string(4) "zero"
}


2 BenutzerBeiträge:
- Beiträge aktualisieren...
pvenakis at efront dot gr
10.01.2008 9: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.05.2007 20: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) ;

?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)