468,306 Members | 1,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

function to extract key/value pairs?

I've searched and searched for a function to create an array from a
string maintaining key/value pairs and keep coming up blank. This
seems to me that it would be quite commonly used.

What I am doing is trying to pass an array using $_GET. To pass the
array, it is rather simple to add it to my get string breaking the
array into key,value,key,value,key,value. The problem is reading it
back into an array. I've tried explode but that places each key and
value as a value in an array. I've tried split and think it might work
except it chokes if there is only one key/value. Is there a function
to do what I want or is there a better way to pass an array?

--
Stan McCann, RETIRED!!, "Uncle Pirate" http://stanmccann.us/
Implementing negative score for googlegroup postings, see
http://blinkynet.net/comp/uip5.html
A zest for living must include a willingness to die. - R.A. Heinlein
Oct 25 '06 #1
3 2640

Stan McCann wrote:
I've searched and searched for a function to create an array from a
string maintaining key/value pairs and keep coming up blank. This
seems to me that it would be quite commonly used.

What I am doing is trying to pass an array using $_GET. To pass the
array, it is rather simple to add it to my get string breaking the
array into key,value,key,value,key,value. The problem is reading it
back into an array. I've tried explode but that places each key and
value as a value in an array. I've tried split and think it might work
except it chokes if there is only one key/value. Is there a function
to do what I want or is there a better way to pass an array?

--
Yes there is a better way to pass an array. Just pass an array :)

page.php?foo[0]=bar&foo[1]=baz&foo[2]=bah

Then $_GET['foo'] will be an array.

Oct 25 '06 #2
Hi Stan --

Perhaps you could use a combination of serialize and base64_encode to
solve your problem. The only catch is that encoding strings in base64
can result in a really long URL and may (ultimately) exceed some
browsers' limits. But if the array you are working with is relatively
small, this solution may be feasible.

First serialize your array, then encode it in base64. Send the encoded
string as your GET variable. On the other side, decode the GET
variable, then unserialize it. This should reassemble your array
correctly.

Geoffrey
Stan McCann wrote:
I've searched and searched for a function to create an array from a
string maintaining key/value pairs and keep coming up blank. This
seems to me that it would be quite commonly used.

What I am doing is trying to pass an array using $_GET. To pass the
array, it is rather simple to add it to my get string breaking the
array into key,value,key,value,key,value. The problem is reading it
back into an array. I've tried explode but that places each key and
value as a value in an array. I've tried split and think it might work
except it chokes if there is only one key/value. Is there a function
to do what I want or is there a better way to pass an array?

--
Stan McCann, RETIRED!!, "Uncle Pirate" http://stanmccann.us/
Implementing negative score for googlegroup postings, see
http://blinkynet.net/comp/uip5.html
A zest for living must include a willingness to die. - R.A. Heinlein
Oct 26 '06 #3
"Geoffrey" <go***********@yahoo.comwrote in
news:11**********************@b28g2000cwb.googlegr oups.com:
Hi Stan --

Perhaps you could use a combination of serialize and base64_encode
to solve your problem. The only catch is that encoding strings in
base64 can result in a really long URL and may (ultimately) exceed
some browsers' limits. But if the array you are working with is
relatively small, this solution may be feasible.

First serialize your array, then encode it in base64. Send the
encoded string as your GET variable. On the other side, decode the
GET variable, then unserialize it. This should reassemble your
array correctly.
That seems to do the trick. Thanks a bunch.

--
Stan McCann, RETIRED!!, "Uncle Pirate" http://stanmccann.us/
Implementing negative score for googlegroup postings, see
http://blinkynet.net/comp/uip5.html
A zest for living must include a willingness to die. - R.A. Heinlein
Oct 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Csaba Gabor | last post: by
44 posts views Thread by RB | last post: by
7 posts views Thread by fox | last post: by
25 posts views Thread by hifrnds007 | last post: by
1 post views Thread by Edwin.Madari | last post: by
19 posts views Thread by JRough | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.