Re: Recursive array_push?

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

 



On Thu, January 26, 2006 2:50 am, Kim Christensen wrote:
> Has anyone out there stumbled over the issue of making
> multi-dimensional arrays out of bracket-separated strings? In a less
> fuzzy way of putting it:
>
> $str = "[layer1][layer2][layer3][layer4]"
>
> would become
>
> $str_array = Array( [layer1] => Array( [layer2] => Array( [layer3] =>
> Array( [layer4] ) ) ) );
>
> ...or something similar. Passing values is not an issue at the moment,
> it's just the recursive thinking that keeps bugging me right now, and
> my temporary solution to this matter is not even mentionable!

To keep in theme with the hack-o-rama, even though I think it's a
lousy way to do this...

$str = substr($str, 1, -1); //chop off lead/end square brackets
$layers = explode('][', $str);
$str_array = array();
$layers = array_reverse($layers);
foreach($layers as $layer){
  $str_array[] = $layer;
  $str_array = array($str_array);
  //or something like that...
}

> I would really like PHP to have a function of building expressions
> with strings, and then execute them through a function - but that's
> just me it seems :-)

I think you mean this one:
http://php.net/eval
:-)

Though the other suggestion might be more suitable to what you are doing:
http://php.net/create_function

You may want to post the Big Picture of what you are doing and how you
got that string in the first place, because 99 times out of 100, when
people use eval, there's an easier way to do it.

-- 
Like Music?
http://l-i-e.com/artists.htm

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