Re: Please, Help dynamicaly creating an Array

From: Date: Wed, 18 Sep 2002 08:54:08 +0000
Subject: Re: Please, Help dynamicaly creating an Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-116691@lists.php.net to get a copy of this message
Try eval(), like this: $str= "'THREE'=>3, 'FOUR'=>4"; eval("\$data2 = array('ONE'=>1,'TWO'=>2,$str);"); Max Sullivan wrote:
I am trying to populate array values and keys from a variable with no luck. Lets say I have the following array. $data1 = array('ONE'=>1,'TWO'=>2,'THREE'=>3,'FOUR'=>4); And I want to create part of the array with the string below: $str= "'THREE'=>3, 'FOUR'=>4"; $data2 = array('ONE'=>1,'TWO'=>2,$str); How can I create $data2 to work the same as $data1. When I try the above for $data2 a new key is created ([0]) for $str. And I end up with ...'TWO'=>2, [0] => 'THREE'=>3, 'FOUR'=>4. It doesn't "interpret" the variable how I expect it to, instead it see's $str as a value. I guess the question is how can I make php use the string literally. I've tried everything I can think of and I think my head is about to explode :). Is it possible to create an Array this way? Any help is appreciated!! print_r($data1): Array ( [ONE] => 1 [TWO] => 2 [THREE] => 3 [FOUR] => 4 ) print_r($data2): Array ( [ONE] => 1 [TWO] => 2 [0] => 'THREE'=>3, 'FOUR'=>4 )


« previous php.general (#116691) next »