简体   繁体   中英

Getting array param out of query string with PHP

( 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

Your query string should rather look like this:

?formparts[]=a&formparts[]=b&formparts[]=c

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM