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

search for in the

ArrayIterator::rewind> <ArrayIterator::offsetSet
[edit] Last updated: Fri, 17 May 2013

view this page in

ArrayIterator::offsetUnset

(PHP 5 >= 5.0.0)

ArrayIterator::offsetUnsetDestruye el valor de un índice

Descripción

public void ArrayIterator::offsetUnset ( string $index )

Destruir el valor de un índice dado.

Advertencia

Esta función no está documentada actualmente, solamente se encuentra disponible la lista de parámetros.

Parámetros

index

El índice a ser destruido.

Valores devueltos

No devuelve ningún valor.

Ver también



add a note add a note User Contributed Notes ArrayIterator::offsetUnset - [2 notes]
up
0
olav at fwt dot no
1 year ago
When unsetting elements as you go it will not remove the second index of the Array being worked on. Im not sure exactly why but there is some speculations that when calling unsetOffset(); it resets the pointer aswell.

<?php

$a
= new ArrayObject( range( 0,9 ) );
$b = new ArrayIterator( $a );

for (
$b->rewind(); $b->valid(); $b->next() )
{
    echo
"#{$b->key()} - {$b->current()} - \r\n";
   
$b->offsetUnset( $b->key() );
}

?>

To avoid this bug you can call offsetUnset in the for loop

<?php
/*** ... ***/
for ( $b->rewind(); $b->valid(); $b->offsetUnset( $b->key() ) )
{
/*** ... ***/
?>

Or unset it directly in the ArrayObject
<?php
/*** ... ***/
   
$a->offsetUnset( $b->key() );
/*** ... ***/
?>

which will produce correct results
up
0
Adil Baig @ AIdezigns
1 year ago
Make sure you use this function to unset a value. You can't access this iterator's values as an array. Ex:

<?php
$iterator
= new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));

foreach(
$iterator as $key => $value)
{
    unset(
$iterator[$key]);
}
?>

Will return :

PHP Fatal error:  Cannot use object of type RecursiveIteratorIterator as array

offsetUnset works properly even when removing items from nested arrays.

 
show source | credits | sitemap | contact | advertising | mirror sites