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 kim.christensen@xxxxxxxxx
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php