开发者

A $_GET input parameter that is an Array

开发者 https://www.devze.com 2022-12-31 20:31 出处:网络
I\'m trying to pass 3 parameter to a script, where the 3rd parameter $_GET[\'value3\'] is supposed to be an array

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 m开发者_StackOverflow中文版y methodology is correct. Can some clarify how you'd go about doing the sort of thing


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.


try

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


For arrays you need to pass the query parameters as

value3[]=abc&value3[]=pqr&value3[]=xyz


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' );


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消