Array references - how to unset() ?

Array references - how to unset() ?

am 03.09.2009 00:28:58 von Martin Zvarik

$ARR = array(
'a' => array('b' => 'blah')
);


function set($key)
{
global $ARR;

foreach ($key as $i => $k) {
if ($i == 0) {
$sourcevar =& $ARR[$k];
} else {
$sourcevar =& $sourcevar[$k];
}
}

// unset($sourcevar); // will cancel the reference - we want to
unset the ['b'], but how?

$sourcevar = null; // will set it NULL, but won't unset...


foreach ($ARR ... // I could run a cleanup, that would go through all of
the array and unset what is NULL, but I would need to use REFERENCES
again!! array_walk_recursive() is also worthless... any ideas?


}

set( array('a', 'b') );





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 00:43:13 von Robert Cummings

Martin Zvarík wrote:
> $ARR = array(
> 'a' => array('b' => 'blah')
> );
>
>
> function set($key)
> {
> global $ARR;
>
> foreach ($key as $i => $k) {
> if ($i == 0) {
> $sourcevar =& $ARR[$k];
> } else {
> $sourcevar =& $sourcevar[$k];
> }
> }
>
> // unset($sourcevar); // will cancel the reference - we want to
> unset the ['b'], but how?
>
> $sourcevar = null; // will set it NULL, but won't unset...
>
>
> foreach ($ARR ... // I could run a cleanup, that would go through all of
> the array and unset what is NULL, but I would need to use REFERENCES
> again!! array_walk_recursive() is also worthless... any ideas?
>
>
> }
>
> set( array('a', 'b') );

unset( $ARR[$k] )

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 00:55:44 von Martin Zvarik

Robert Cummings napsal(a):
>
>
> Martin Zvarík wrote:
>> $ARR = array(
>> 'a' => array('b' => 'blah')
>> );
>>
>>
>> function set($key)
>> {
>> global $ARR;
>>
>> foreach ($key as $i => $k) {
>> if ($i == 0) {
>> $sourcevar =& $ARR[$k];
>> } else {
>> $sourcevar =& $sourcevar[$k];
>> }
>> }
>>
>> // unset($sourcevar); // will cancel the reference - we want to
>> unset the ['b'], but how?
>>
>> $sourcevar = null; // will set it NULL, but won't unset...
>>
>>
>> foreach ($ARR ... // I could run a cleanup, that would go through all
>> of the array and unset what is NULL, but I would need to use
>> REFERENCES again!! array_walk_recursive() is also worthless... any
>> ideas?
>>
>>
>> }
>>
>> set( array('a', 'b') );
>
> unset( $ARR[$k] )
>
> Cheers,
> Rob.
Thanks for reply, but I want to:

unset($ARR['a']['b'])

Imagine I have this:

$KEYS = array('a', 'b', 'c');

And I want to:

unset($ARR['a']['b']['c'])


It's probably impossible, unless I do something dirty like this:

list($rootA, $rootB, $rootC) = $KEYS;

if (isset($rootC)) unset($x[$rootA][$rootB][$rootC]);
elseif (isset($rootB)) unset($x[$rootA][$rootB]);
elseif (isset($rootA)) unset($x[$rootA]);






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 02:40:03 von Robert Cummings

Martin Zvarík wrote:
> Robert Cummings napsal(a):
>>
>> Martin Zvarík wrote:
>>> $ARR = array(
>>> 'a' => array('b' => 'blah')
>>> );
>>>
>>>
>>> function set($key)
>>> {
>>> global $ARR;
>>>
>>> foreach ($key as $i => $k) {
>>> if ($i == 0) {
>>> $sourcevar =& $ARR[$k];
>>> } else {
>>> $sourcevar =& $sourcevar[$k];
>>> }
>>> }
>>>
>>> // unset($sourcevar); // will cancel the reference - we want to
>>> unset the ['b'], but how?
>>>
>>> $sourcevar = null; // will set it NULL, but won't unset...
>>>
>>>
>>> foreach ($ARR ... // I could run a cleanup, that would go through all
>>> of the array and unset what is NULL, but I would need to use
>>> REFERENCES again!! array_walk_recursive() is also worthless... any
>>> ideas?
>>>
>>>
>>> }
>>>
>>> set( array('a', 'b') );
>> unset( $ARR[$k] )
>>
>> Cheers,
>> Rob.
> Thanks for reply, but I want to:
>
> unset($ARR['a']['b'])
>
> Imagine I have this:
>
> $KEYS = array('a', 'b', 'c');
>
> And I want to:
>
> unset($ARR['a']['b']['c'])

This is possible. You're just not giving enough consideration to your
exit strategy :)


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.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 03:04:46 von Martin Zvarik

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 :)
>
> >
> 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

Re: Array references - how to unset() ?

am 03.09.2009 16:06:25 von hack988 hack988

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=EDk :
> AHA !!!
>
> OMG... how come I did not see that!?
>
> Instead this:
> $array =3D& $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 ex=
it
>> strategy :)
>>
>> >>
>> function unset_deep( &$array, $keys )
>> {
>> =A0 =A0$final =3D array_pop( $keys );
>>
>> =A0 =A0foreach( $keys as $key )
>> =A0 =A0{
>> =A0 =A0 =A0 =A0$array =3D &$array[$key];
>> =A0 =A0}
>>
>> =A0 =A0unset( $array[$final] );
>> }
>>
>> $value =3D array
>> (
>> =A0 =A0'a' =3D> array
>> =A0 =A0(
>> =A0 =A0 =A0 =A0'b' =3D> array
>> =A0 =A0 =A0 =A0(
>> =A0 =A0 =A0 =A0 =A0 =A0'c' =3D> 'C',
>> =A0 =A0 =A0 =A0 =A0 =A0'd' =3D> 'D',
>> =A0 =A0 =A0 =A0 =A0 =A0'e' =3D> 'E'
>> =A0 =A0 =A0 =A0),
>> =A0 =A0),
>> );
>>
>> $keys =3D 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
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 16:21:29 von Robert Cummings

hack988 hack988 wrote:
> Reference vars in php would not be unset if it reference by another
> var,so you must keep original var had'nt being reference.

You can't reference a reference in PHP. If you take the reference of a
variable that is itself a reference then you are taking a reference to
the referenced value and not the reference variable itself. This is
illustrated in the following example:


$foo = '123';
$fee = '987';

$blah = &$foo;
$bleh = &$blah;
$blah = &$fee;

echo 'blah: '.$blah."\n";
echo 'bleh: '.$bleh."\n";

?>

What I think you meant to say, is that a value is not unset as long as
it has at least one variable referencing it. You most certainly unset
the reference if you apply unset to it, it is the value referenced that
may remain.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Array references - how to unset() ?

am 03.09.2009 17:30:07 von hack988 hack988

Yes,thanks and I sorry for my poor english

2009/9/3 Robert Cummings :
> hack988 hack988 wrote:
>>
>> Reference vars in php would not be unset if it reference by another
>> var,so you must keep original var had'nt being reference.
>
> You can't reference a reference in PHP. If you take the reference of a
> variable that is itself a reference then you are taking a reference to the
> referenced value and not the reference variable itself. This is illustrated
> in the following example:
>
> >
> $foo = '123';
> $fee = '987';
>
> $blah = &$foo;
> $bleh = &$blah;
> $blah = &$fee;
>
> echo 'blah: '.$blah."\n";
> echo 'bleh: '.$bleh."\n";
>
> ?>
>
> What I think you meant to say, is that a value is not unset as long as it
> has at least one variable referencing it. You most certainly unset the
> reference if you apply unset to it, it is the value referenced that may
> remain.
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php