Re: Recursive array_push?

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

 



Kim,

After some contemplation (and slightly less crack):

<?php
$str   = "[layer1][layer2][layer3][layer4]";
$parts = array_reverse(explode("][", substr($str, 1, -1)));
$array = "FOO";
foreach ($parts as $part) {
    $array = array($part => $array);
}
print_r($array);
?>

Array
(
    [layer1] => Array
        (
            [layer2] => Array
                (
                    [layer3] => Array
                        (
                            [layer4] => FOO
                        )

                )

        )

)

David

David Grant wrote:
> Kim,
> 
> May the hack-o-rama commence:
> 
> <?php
> $str   = "[layer1][layer2][layer3][layer4]";
> $parts = explode("][", substr($str, 1, -1));
> $text  = "";
> foreach ($parts as $part) {
>     $text .= 'a:1:{s:' . strlen($part) . ':"' . $part . '";';
> }
> $text .= 'b:1;' . str_repeat('}',  count($parts));
> print_r(unserialize($text));
> ?>
> 
> It works, but I'm not proud. :P
> 
> David


-- 
David Grant
http://www.grant.org.uk/

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