Re: Named pair array from variable

From: Date: Mon, 29 Jun 2009 23:38:13 +0000
Subject: Re: Named pair array from variable
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-294725@lists.php.net to get a copy of this message
Shawn McKenzie wrote: > Mikey Knutson wrote: >> Is this even possible? I'm building a string, properly formatted, to create >> a named pair or associative array. The string is fine, and when I use it >> directly to create the array, all is well. When I try to use the var to >> create the array, I get an empty array (I think). Huh? >> >> Here is what I have: >> >> $myString = "'username' => 'password' , 'mickey' >> => 'mouse' , 'donald' => >> 'duck'"; >> $myArray = array($myString); >> print ("array val: $myArray[username]"); // get an empty string here >> >> >> > > AFAIK you can't do what you're showing. This will work, but is not advised: > > eval('$myArray = array(' . $myString . ');'); > print ("array val: $myArray[username]"); // array val: password > > Why are you needing to store the array indexes and values in a string > like this? Maybe we can give you an alternative approach. > Unless I'm way off you might like serialize() -- Thanks! -Shawn http://www.spidean.com

« previous php.general (#294725) next »