Re: [PHP] Recursive array_push?

2006-01-26 Thread Robin Vickery
On 1/26/06, Kim Christensen [EMAIL PROTECTED] wrote:

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


You mean like create_function() ?

  -robin


Re: [PHP] Recursive array_push?

2006-01-26 Thread David Grant
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



Re: [PHP] Recursive array_push?

2006-01-26 Thread Jochem Maas

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!

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


you will have to refactor the code below - I cut and paste it from a
session wrapper [class] I wrote so it won't work as is (and probably your
not wanting to do something specifically with the session)  - also its a little
different in that rather than using a string like:

 $str = [layer1][layer2][layer3][layer4]

it runs off off an array like so:

 $str = array('layer1','layer2','layer3','layer4');

// {{{ set

/**
 * set an item in the SESSION array (mutli-dim)
 *
 * The $varName param can be an array in which case each
 * value in the array is a key in one level of a multidimentional
 * array e.g.
 *  array('my','var','here');
 * would point to:
 *  $_SESSION['my']['var']['here'];
 *
 * we don't allow numeric indexes because 'they are a head fuck'
 * - meaning you'll never remember what the value was about and the
 * chance of overwrite another numeric index is in my approximation quite 
high
 *
 * @param   $varName string/array
 * @param   $value   mixed
 *
 * @return  $value on success / false overwise (Exception on numeric key.)
 */
static public function set($varName, $value = null)
{
if (self::$started  $varName  !is_numeric($varName)) {
if (is_array( $varName )) {
$tmpArr = $_SESSION;
while ( 1 ) {
self::chkSessionVarName($k = array_shift( $varName ));
if ( !count( $varName )) {
return ($tmpArr[ $k ] = $value);
break;
} else if (! isset($tmpArr[ $k ]) || ! is_array($tmpArr[ $k 
])) {
$tmpArr[ $k ] = array();
}

$tmpArr = $tmpArr[ $k ];
}
} else {
self::chkSessionVarName($varName);
return ($_SESSION[ $varName ] = $value);
}
}
}

// }}}
// {{{

/**
 * get an item in the SESSION array (multi-dim)
 *
 * The $varName param can be an array in which case each
 * value in the array is a key in one level of a multidimentional
 * array e.g.
 *  array('my','var','here');
 * would point to:
 *  $_SESSION['my']['var']['here'];
 *
 * we don't allow numeric indexes because 'they are a head fuck'
 *
 * @param   $varName string/array
 *
 * @return  mixed
 */
static public function get($varName)
{
self::$lastGetFailed = true;
if (self::$started  $varName) {
if (is_array( $varName )) {
$tmpArr = $_SESSION;
while ( 1 ) {
self::chkSessionVarName($k = array_shift( $varName ));

/* endpoint */
if ( !count( $varName )) {
if (@is_array($tmpArr)  array_key_exists($k, 
$tmpArr)) {
self::$lastGetFailed = false;
return $tmpArr[ $k ];
}
break;
}
else if (!array_key_exists($k, $tmpArr) ||
 !is_array($tmpArr[ $k ]))
{
// we can go no deeper
break;
}

$tmpArr = $tmpArr[ $k ];
}
} else {
self::chkSessionVarName( $varName );
if (array_key_exists($varName, $_SESSION)) {
self::$lastGetFailed = false;
return $_SESSION[ $varName ];
}
}
}

return null;
}



--
Kim Christensen
[EMAIL PROTECTED]



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



Re: [PHP] Recursive array_push?

2006-01-26 Thread David Grant
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



Re: [PHP] Recursive array_push?

2006-01-26 Thread Richard Lynch
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