开发者

how to pass an array in GET in PHP?

开发者 https://www.devze.com 2023-02-13 14:37 出处:网络
$idArray = array(1,2,3,4); can开发者_JS百科 I write this line in HTML? <form method=\'POST\' action=\'{$_SERVER[\'PHP_SELF\']}?arr={$idArray}\'>
$idArray = array(1,2,3,4);

can开发者_JS百科 I write this line in HTML?

<form method='POST' action='{$_SERVER['PHP_SELF']}?arr={$idArray}'>

or should I write:

<form method='POST' action='{$_SERVER['PHP_SELF']}?arr[]={$idArray}'>

how will it be passed?

how should I handle it in the called page?

thanks !!


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.


Use serialize and unserialize PHP function. This function giving you storable (string) version of array type. For more infomation about usage read http://php.net/manual/en/function.serialize.php and http://www.php.net/manual/en/function.unserialize.php


You could use serialize and & serialize along side with urlencode e.g.

On Sending you can send them like these:

<?php
$array1 = Array(["key1"]=>"value1",["key2"]=>"value2");
$array2 = Array(["key1"]=>"value1",["key2"]=>"value2");
$data1="textdata";

$urlPortion= '&array1='.urlencode(serialize($array1)).
             '&array2='.urlencode(serialize($array2)).
             '&data1='.urlencode(serialize($data1));
//Full URL:
$fullUrl='http://localhost/?somevariable=somevalue'.$urlPortion
?>

On Receiving you can access them as:

<?php
$destArray1=unserialize($_GET['array1']);
$destArray2=unserialize($_GET['array2']);
$destData1=unserialize($_GET['data1']);
?>

And Voila, you can attach that url on ajax request or normal browser page.


Another option (even nice looking, I'd say):

<form method='POST'>
  <input type="hidden" name="idArray[]" value="1" />
  <input type="hidden" name="idArray[]" value="2" />
  <input type="hidden" name="idArray[]" value="3" />
  <input type="hidden" name="idArray[]" value="4" />
</form>

But of course it gets sent as POST. I wouldn'r recommend sending it with serialize since the output of that function can get pretty big and the length or URL is limited.

with GET:

<form method='GET'>
      <input type="hidden" name="idArray[]" value="1" />
      <input type="hidden" name="idArray[]" value="2" />
      <input type="hidden" name="idArray[]" value="3" />
      <input type="hidden" name="idArray[]" value="4" />
</form>


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.


Use parse_str function

$str = "first=value&arr[]=foo+bar&arr[]=baz"; 
parse_str($str); 
echo $first;  


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


serialize() your array first and pass that through. Then call unserialize() on it. http://ie.php.net/serialize


A session is a much safer and cleaner way to do this. Start your session with:

session_start();

Then add your serialized array as a session variable like this:

$_SESSION["venue"] = serialize($venue);

The simply call up the session variable when you need it.


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.

0

精彩评论

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