Is it possible to have a value in $_GET
as an array?
If I am trying to send a link with http://link/foo.php?id=1&id=2&id=3
, and I want to use $_GET['id']
on the php side, how can that value be an array? Because right now echo $_GET['id']
is returning 3
. Its the last id which is in the header link. Any suggestions?
You can get them using the Query String:
This will give you:
Then
This will give you
When you don't want to change the link (e.g.
foo.php?id=1&id=2&id=3
) you could probably do something like this (although there might be a better way...):Yes, here's a code example with some explanation in comments:
The usual way to do this in PHP is to put
id[]
in your URL instead of justid
:Then
$_GET['id']
will be an array of those values. It's not especially pretty, but it works out of the box.You can specify an array in your HTML this way:
This will result in this $_GET array in PHP:
Of course, you can use any sort of HTML input, here. The important thing is that all inputs whose values you want in the 'id' array have the name
id[]
.I think i know what you mean, if you want to send an array through a URL you can use serialize
for example:
and on page.php