A $_GET input parameter that is an Array

2019-07-13 02:34发布

I'm trying to pass 3 parameter to a script, where the 3rd parameter $_GET['value3'] is supposed to be an array

$_GET['value1'] 
$_GET['value2'] 
$_GET['value3'] //an array of items

I'm calling the script like this: (notice my syntax for value3, I'm not sure it's correct)

http://localhost/test.php?value1=test1&value2=test2&value3=[the, array, values]

I then use a foreach to hopefully loop through the third parameter value3 which is the array

//process the first input $_GET['value1']

//process the second input $_GET['value2']

//process the third input $_GET['value3'] which is the array
foreach($_GET['value3'] as $arrayitem){
    echo $arrayitem; 
}

but I get the error Invalid argument supplied for foreach()

I'm not sure if my methodology is correct. Can some clarify how you'd go about doing the sort of thing

标签: php arrays get
6条回答
2楼-- · 2019-07-13 02:52

There is no such thing as "passing an array as a URL parameter" (or a form value, for that matter, because this is the same thing). These are strings, and anything that happens to them beyond that is magic that has been built into your application server, and therefore it is non-portable.

PHP happens to support the &value3[]=the&value3[]=array&value3[]=values notation to automagically create $_GET['value3'] as an array for you, but this is special to PHP and does not necessarily work elsewhere.

You can also be straight-forward and go for a cleaner URL, like this: value3=the,array,values, and then use explode(',', $_GET['value3']) in your PHP script to create an array. Of course this implies that your separator char cannot be part of the value.

To unambiguously transport structured data over HTTP, use a format that has been made for the purpose (namely: JSON) and then use json_decode() on the PHP side.

查看更多
何必那么认真
3楼-- · 2019-07-13 02:52

http://php.net/manual/en/reserved.variables.get.php Check out the above link.. You will see how the GET method is implemented. What happens is that the URL is taken, it is delimited using '&' and then they are added as a key-value pair.

   public function fixGet($args) {
    if(count($_GET) > 0) {
        if(!empty($args)) {
            $lastkey = "";
            $pairs = explode("&",$args);
            foreach($pairs as $pair) {
                if(strpos($pair,":") !== false) {
                    list($key,$value) = explode(":",$pair);
                    unset($_GET[$key]);
                    $lastkey = "&$key$value";
                } elseif(strpos($pair,"=") === false)
                    unset($_GET[$pair]);

                else {
                    list($key, $value) = explode("=",$pair);
                    $_GET[$key] = $value;
                }
            }
        }
        return "?".((count($_GET) > 0)?http_build_query($_GET).$lastkey:"");
    }

Since, they are added as a key-value pair you can't pass array's in the GET method...

查看更多
别忘想泡老子
4楼-- · 2019-07-13 02:56

The following would also work:

http://localhost/test.php?value3[]=the&value3[]=array&value3[]=values

A more advanced approach would be to serialize the PHP array and print it in your link:

http://localhost/test.php?value3=a:3:{i:0;s:3:"the";i:1;s:5:"array";i:2;s:6:"values";}

would, essentially, also work.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-07-13 02:57

For arrays you need to pass the query parameters as

value3[]=abc&value3[]=pqr&value3[]=xyz
查看更多
你好瞎i
6楼-- · 2019-07-13 03:02

You can cast the name of the index in the string too

?value1[a]=test1a&value1[b]=test1b&value2[c][]=test3a&value2[c][]=test3b

would be

$_GET['value1']['a'] = test1a
$_GET['value1']['b'] = test1b
$_GET['value2']['c'] = array( 'test3a', 'test3b' );
查看更多
你好瞎i
7楼-- · 2019-07-13 03:04

try

http://localhost/test.php?value1=test1&value2=test2&value3[]=the&value3[]=array&value3[]=values
查看更多
登录 后发表回答