tags:

views:

152

answers:

6

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

+7  A: 

try

http://localhost/test.php?value1=test1&value2=test2&value3[]=the&value3[]=array&value3[]=values
Jaroslav Záruba
+2  A: 

For arrays you need to pass the query parameters as

value3[]=abc&value3[]=pqr&value3[]=xyz
sushil bharwani
You should probably remove those single quotes.
webbiedave
+6  A: 

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.

Tomalak
+1 for great thorough explanation, and specifics to app server.
Brian Wigginton
What makes `value3=the,array,values` cleaner? It may be cleaner to the eye but it's certainly not cleaner to the code.
webbiedave
I think relying on a language feature when developing in that language isn't all too bad, lol..
Dan Heberden
@webbiedave: Clean URLs > not-so-clean URLs. And "cleaner in the code" certainly depends on from where you look. I'd say a URL with commas is easier to generate than one in the fancy PHP notation, resulting in cleaner code.
Tomalak
It probably wouldn't be terribly difficult to implement PHP's style of URL parsing in another language, should you attempt to rewrite your entire application without changing any URLs.
Frank Farmer
@Dan Heberden: No, of course not. That was not my point. But these magic features of PHP make it all-too-easy to believe you *actually* could transport an array via URL parameters (I've seen that notion several times here), and I thought it would be a good thing to say that this is not *actually* happening. Having a clean distinction between what PHP does and what HTTP does is important, IMHO.
Tomalak
@Tomalak: and glad you pointed it out :)
Dan Heberden
Separating with a comma is, and should be emphasized strongly as, not feasible. Using a humanly introduced delimiter to delimit the distinct variables is a very bad idea -- especially in this case. A user may run in all sorts of trouble for entering a value that breaks the program.Moreover, it's a bit nasty to handle logic on the client side to send away the variables delimited this way.PHP handled the shortcut of acknowledging multi-dimensional data _that way_ for a reason. Let's not get carried away.
msakr
@mahmoudsakr: This very much depends on whether the separator can be natural part of the values or not. And as the programmer, I should think you will be able to predict this. ;) In any case, I'm not saying there is anything wrong with PHP's way of handling this, it is perfectly fine. I'm saying that it's not a feature of HTTP, and it is not the only way of doing this kind of stuff, once you know that it all boils down to plain strings.
Tomalak
Personally I would not pass large JSON objects in the GET query string - you run the chance of hitting the HTTP query string character limit. However this does make the URL's cross platform. Also remember to `urlencode()`
Geek Num 88
@Geek: If you are in these regions, I predict that the PHP array notation fails a lot earlier than the JSON array notation because it is much more repetitive. In any case, you are absolutely right about paying attention to GET request limits and URL encoding.
Tomalak
+1  A: 

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' );
Dan Heberden
this is real new stuff :-)
sushil bharwani
A: 

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.

msakr
The first form doesn't work in PHP. If the same key shows up multiple times, PHP uses the last value assigned. An array append is only done if `[]` chars are present in the key.
Frank Farmer
A: 

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

Vimard