Getting array param out of query string with PHP
Asked Answered
G

2

1

(NOTE: This is a follow up to a previous question, How to pass an array within a query string?, where I asked about standard methods for passing arrays within query strings.)

I now have some PHP code that needs to consume the said query string- What kind of query string array formats does PHP recognize, and do I have to do anything special to retrieve the array?

The following doesn't seem to work:

Query string:

?formparts=[a,b,c]

PHP:

$myarray = $_GET["formparts"];
echo gettype($myarray)

result:

string
Glow answered 5/6, 2011 at 15:4 Comment(3)
next time try to explain better your question, because as now it's unreadableOzalid
There's only one type of array in php, an "array". The format depends on what you put into them.Double
@yes123, @MarcB- please see revised questionGlow
G
3

Your query string should rather look like this:

?formparts[]=a&formparts[]=b&formparts[]=c
Glori answered 5/6, 2011 at 15:12 Comment(0)
F
0

If you're dealing with a query string, you are looking at the $_GET variable. This will contain everything after the ? in your previous question.

So what you will have to do is pretty much the opposite of the other question.

$products = array();
// ... Add some checking of $_GET to make sure it is sane
....
// then assign..
$products = explode(',', $_GET['pname']);

and so on for each variable. I must give you a full warning here, you MUST check what comes through the $_GET variable to make sure it is sane. Otherwise you risk having your site compromised.

Falgout answered 5/6, 2011 at 15:12 Comment(2)
@KyleWppd- I'm talking about passing arrays WITHIN query strings, not the query string GET array itself- See @soulmerge's answerGlow
By definition in PHP, and variable passed as part of a $_GET or $_POST request is a string. I can't speak to whether soulmerge's answer will work or not, but he is using a different syntax to assign into the array than you are.Falgout

© 2022 - 2024 — McMap. All rights reserved.