Re: Array references - how to unset() ?

From: Date: Thu, 03 Sep 2009 14:06:25 +0000
Subject: Re: Array references - how to unset() ?
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-297631@lists.php.net to get a copy of this message
Reference vars in php would not be unset if it reference by another var,so you must keep original var had'nt being reference. 2009/9/3 Martin Zvarík <mzvarik@gmail.com>: > AHA !!! > > OMG... how come I did not see that!? > > Instead this: > $array =& $array[$final]; > unset($array); > > This: > unset($array[$final]); > > > 3 AM in the morning... that must be the reason! .) > > Thanks. > > > >> >> This is possible. You're just not giving enough consideration to your exit >> strategy :) >> >> <?php >> >> function unset_deep( &$array, $keys ) >> { >>    $final = array_pop( $keys ); >> >>    foreach( $keys as $key ) >>    { >>        $array = &$array[$key]; >>    } >> >>    unset( $array[$final] ); >> } >> >> $value = array >> ( >>    'a' => array >>    ( >>        'b' => array >>        ( >>            'c' => 'C', >>            'd' => 'D', >>            'e' => 'E' >>        ), >>    ), >> ); >> >> $keys = array('a', 'b', 'c'); >> unset_deep( $value, $keys ); >> >> print_r( $value ); >> >> ?> >> >> Cheers, >> Rob. > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.general (#297631) next »