Re: Array references - how to unset() ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

<?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.
--
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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux