Re: create multidimensional array with n depth

From: Date: Wed, 09 Aug 2006 19:33:17 +0000
Subject: Re: create multidimensional array with n depth
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-240409@lists.php.net to get a copy of this message
On Wed, 2006-08-09 at 14:14 -0400, tg-php@gryffyndevelopment.com wrote: > 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? Yep, that's pretty bad use of eval() :) Instead use recursion or a sliding reference: <?php $key = '1_20-2_16-7_14'; $val = 12; $bits = split( '_|-', $key ); $nested = array(); $ref = &$nested; $lastref = null; foreach( $bits as $bit ) { $lastRef = &$ref; $ref[$bit] = array(); $ref = &$ref[$bit]; } if( $lastRef === null ) { $nested = $val; } else { $lastRef[$bit] = $val; } print_r( $nested ); ?> Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#240409) next »