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
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 useexplode(',', $_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.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.
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.
For arrays you need to pass the query parameters as
You can cast the name of the index in the string too
would be
try