$idArray = array(1,2,3,4);
can I write this line in HTML?
If you want to pass an array as parameter, you would have to add a parameter for each element. Your query string would become:
?arr[]=1&arr[]=2&arr[]=3&arr[]=4
As others have written, you can also serialize and unserialize the array.
But do you really have to send the data to the client again? It looks like you just need a way to persist the data between requests.
In this case, it is better imo to use sessions(docs). This is also more secure as otherwise the client could modify the data.
Just use explode() and pass it's value. You can get the array back by using implode().
Note: Choose the delimiter according to the type of content that does not exist in your array. For eg. If you are sure that there won't be any commas ( , ) in your array, then pick comma as delimiter.
http://snipplr.com/view/4444/passing-an-array-through-get-request/
$str=serialize($idArray);
<form method='POST' action='{$_SERVER['PHP_SELF']}?arr=$str'>
To get the data in the receiving page you will first have to:
<?PHP
$idArray = unserialize($_GET["arr"]);
?>
In the particular case you mentioned, I would implode the array to a string and then explode it when you post the form.
$str = rawurlencode(implode(",",$idArray));
<form method='POST' action='{$_SERVER['PHP_SELF']}?arr={$str}'>
and then on the post processing:
$idArray = explode(",",rawurldecode($_POST['arr']));
Felix's answer answers the question beautifully, but lacks the examples in my opinion.
This answer is prompted by the comment on Felix's answer.
can you specify keys using this method? – Qwerty Apr 27 '14 at 0:05
First off, to illustrate Felix's answer:
<input type="hidden" name="array[]" value="val1" />
<input type="hidden" name="array[]" value="val2" />
<input type="hidden" name="array[]" value="val3" />
When the request is sent to the server it will be of the type Array
.
Following is an example with keys. This will give you an array with two keys, two values each.
<input type="hidden" name="array['first']" value="val1" />
<input type="hidden" name="array['first']" value="val2" />
<input type="hidden" name="array['second']" value="val3" />
<input type="hidden" name="array['second']" value="val4" />
Finally here's an example with VueJS, which is what I was using as of this writing, which led me to this question.
<input v-for="(value, key) in data" type="hidden" :name="'array[' + key + ']'" :value="value">
I hope this will be helpful to any passersby.
Another option is to json_encode then base64_encode then urlencode then you can pass that into a get request.
$idArray = [1,2,3,4];
$urlArray = urlencode(base64_encode(json_encode($idArray)));
$fullURL = 'https://myserver/mypath/myscript.php?arr=' . $urlArray;
On receiving you can get back to the original array by urldecode then base64_decode then json_decode.
$idArray = json_decode(base64_decode(urldecode($_GET["arr"])));
As other have mentioned you can use serialize and unserialize but it is considered to be more secure to use json_encode and json_decode instead. Also as of PHP7 unserialize has a second parameter, for more info see https://github.com/kalessil/phpinspectionsea/blob/master/docs/security.md#exploiting-unserialize
You may not need to use the base64_encode and base64_decode but I recommend it. It will cost you some processing resources but will result in a shorter URL saving you network resources. Keep in mind that if your working with large arrays you may excede the limits of the allowed length of get requests for your server.