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
3
  • next time try to explain better your question, because as now it's unreadable Commented Jun 5, 2011 at 15:07
  • There's only one type of array in php, an "array". The format depends on what you put into them. Commented Jun 5, 2011 at 15:11
  • @yes123, @MarcB- please see revised question Commented Jun 5, 2011 at 15:12

2 Answers 2

3

Your query string should rather look like this:

?formparts[]=a&formparts[]=b&formparts[]=c
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

@KyleWppd- I'm talking about passing arrays WITHIN query strings, not the query string GET array itself- See @soulmerge's answer
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.