Placing string variables within array fucntion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcf378
Forum Newbie
Posts: 1
Joined: Mon May 10, 2010 4:42 pm

Placing string variables within array fucntion

Post by jcf378 »

Hi - i think there is probably something simple I'm overlooking...
I need to create the following line of php to work within the confines of a more complex project where I have little flexibility about this particular line:

$item = array("sample1"=>"100", "sample2"=>"200", "sample3"=>"300");

Instead of having the text of this array function 'hard-coded', I am trying to fill this array function with a dynamic string variable which I've CURL'd from another page; the result of my CURL is a perfectly-formatted text-string that would fit verbatim into my required array function format:

$resultcurl = ' "sample1"=>"100", "sample2"=>"200", "sample3"=>"300" ' ;

and so i'm trying to get a final working code to look something like:

$item = array($resultcurl);

However, the array function doesn't simply substitute $resultcurl with its associated formatted textstring...
So long story short, is there a way to get the array() function to read and process the textstring as represented by my $resultcurl variable?

many thanks,
-jason fisher
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Placing string variables within array fucntion

Post by cpetercarter »

You could explode() the string on the commas, and then explode() each bit on => to give you the keys and values to put into the array. But it might be simpler to find a way of sending the data in your cURL as an array to start with.
Post Reply