Re: create multidimensional array with n depth
| From: | tg-php at gryffyndevelopment dot com | Date: | Wed, 09 Aug 2006 18:14:55 +0000 |
| Subject: | Re: create multidimensional array with n depth | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-240398@lists.php.net to get a copy of this message | ||
My brain is really fried right now and I'm sure someone has a more indepth answer/opinion on
this subject, but it occurs to me that you can get away from using eval() if you do some kind of
recursive function using:
$temp[] = array($arrval1, $arrval2,...);
and $arrval1 could == array($arr2val1, $arr2val2,...);
using the [] syntax and/or assigning arrays to other array value positions could do what you want it
to do if you can work out the resursive functionality. No need to use strings and eval() all over
the place.
Sorry my head's not up to resursion right now.. plus I never did have as intuitive of a feel
for it as I'd ever have liked (understand the concept and can grok other people's stuff,
but never bothered writing any of my own);
Maybe that's a nudge in the right direction while the others finish lunch or coffee or
whatever.
-TG
= = = Original message = = =
Hi all:
I've been trying to create a multidimensional array with n depth in php.
Tried to use a recursive function passing array references by pointers but
seems like it's not possible or I still don't understand this subject
enough. Finally managed to get it going using the eval function. The code
below converts a seperated string into a multi dimensional array with n
depth:
e.g. $array['1_20-2_16-7_14'] = 12 will become
$eval_array[1][20][2][16][7][14] = 12
foreach(array_keys($this->quantity_array) AS $key)
if($this->quantity_array[$key] > 0)
$combinations = explode('-', $key);
$eval_string = '$eval_array';
foreach(array_keys($combinations) AS $key2)
$option_key_value = explode('_', $combinations[$key2]);
$eval_string .=
'['.$option_key_value[0].']['.$option_key_value[1].']';
$eval_string .= ' = '.$this->quantity_array[$key].';';
eval($eval_string);
Using eval() for it seems a bit expensive to me. So I've been wondering if
there is an easier way?
TIA
Gunter
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.