Re: multiselects and arrays

From: Date: Thu, 10 May 2001 19:23:28 +0000
Subject: Re: multiselects and arrays
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-54789@lists.php.net to get a copy of this message
What I do when I need multidimensional arrays from forms, is name my variables: <INPUT NAME="i::foo::bar" VALUE=" ... "> i.e., I delimit the dimensions with "::". Then, on the page they are posted to, I do: <? parseInputVars('i'); ?> And here is the code for that function, which looks for all the variables that start with 'i::' and creates the multidimensional array needed. <? ############################################################### # # parseInputVars # # - parses HTTP_POST variables into a multi-dimensional # arary, using '::' as the delimiter, and also runs # a function on each element of the array during parsing, # and optionally another function on the entire array # after parsing # function parseInputVars($var, $function=false, $global_function=false) { global $HTTP_POST_VARS, $$var; if (is_array($HTTP_POST_VARS)) { $temp = ''; foreach($HTTP_POST_VARS as $k=>$v) { $s = explode('::',$k); if ($s[0] == $var) { $s2 = array(); foreach ($s as $k2=>$v2) { if ($k2) { $s2[] = $v2; } } if (is_object($v) || is_array($v)) { $temp .= sprintf("\$t[%s] = unserialize(urldecode(\"%s\"));", join('][',$s2), urlencode(serialize($v)) ); } else { if ($function) { $temp .= sprintf("\$t[%s] = %s(urldecode(\"%s\"));", join('][',$s2), $function, urlencode($v) ); } else { $temp .= sprintf("\$t[%s] = urldecode(\"%s\");", join('][',$s2), urlencode($v) ); } } } } eval($temp); $$var = $t; if ($global_function) { $global_function($$var); } } } ?> -- Colin Viebrock Co-Founder, easyDNS Technologies Inc. http://www.easyDNS.com/

« previous php.dev (#54789) next »