How to Pass an Array in Get in PHP

How to pass an array via $_GET in php?

You can use the [] syntax to pass arrays through _GET:

?a[]=1&a[]=2&a[]=3

PHP understands this syntax, so $_GET['a'] will be equal to array(1, 2, 3).

You can also specify keys:

?a[42]=1&a[foo]=2&a[bar]=3

Multidimentional arrays work too:

?a[42][b][c]=1&a[foo]=2

http_build_query() does this automatically:

http_build_query(array('a' => array(1, 2, 3))) // "a[]=1&a[]=2&a[]=3"

http_build_query(array(
'a' => array(
'foo' => 'bar',
'bar' => array(1, 2, 3),
)
)); // "a[foo]=bar&a[bar][]=1&a[bar][]=2&a[bar][]=3"

An alternative would be to pass json encoded arrays:

?a=[1,2,3]

And you can parse a with json_decode:

$a = json_decode($_GET['a']); // array(1, 2, 3)

And encode it again with json_encode:

json_encode(array(1, 2, 3)); // "[1,2,3]"

Dont ever use serialize() for this purpose. Serialize allows to serialize objects, and there is ways to make them execute code. So you should never deserialize untrusted strings.

How to pass an array within a query string?

Here's what I figured out:

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
<select multiple="multiple" name="cars[]">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
</select>
</form>

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

... or as multiple hidden fields with the same name:

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.


UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3

Rails:

"list_a": "3", 
"list_b":[
"1",
"2",
"3"
],
"list_c": "1,2,3"

Angular:

 "list_a": [
"1",
"2",
"3"
],
"list_b[]": [
"1",
"2",
"3"
],
"list_c": "1,2,3"

(Angular discussion)

See comments for examples in node.js, Wordpress, ASP.net


Maintaining order:
One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

How to pass an array into a function, and return the results with an array

You seem to be looking for pass-by-reference, to do that make your function look this way (note the ampersand):

function foo(&$array)
{
$array[3]=$array[0]+$array[1]+$array[2];
}

Alternately, you can assign the return value of the function to a variable:

function foo($array)
{
$array[3]=$array[0]+$array[1]+$array[2];
return $array;
}

$waffles = foo($waffles)

Passing an array to another page (Form)

i'm glad @ksealey pointed out a more proper method of doing this, but for the sake of answering the question...the reason it's not working is that the serialize alone is not enough to prevent the invalid html. see result of what the serialize leaves in the html:

Sample Image

so you need to be sure the html you produce is valid. you might use encoding like base64 to produce safe html:

echo  " <form id=\"my_form\" action=\"\" method=\"post\"";
echo "enctype=\"multipart/form-data\">";
echo "<input type=\"hidden\" name=\"id\" value=\"10\">";
echo "<input type=\"hidden\" name=\"input_name\" ";

echo "value=\"".base64_encode(serialize($my_array))."\" />";

Sample Image

then you can just add the decode to your output:

$passed_array = unserialize(base64_decode($_POST['input_name']));
print_r($passed_array);

How to pass an array to another page with PHP?

You could put it in the session:

session_start();
$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

Pass array for GET data in GuzzlePHP

Found it finally. Settings key is query If anyone else needs it:

$params = [
'var1' => 'value1',
'var4' => 'value4',
];

$client->request('GET', '/redirect/3', [
'query' => $params
]);

This finally transforms the URL of my request into:

/redirect/3?var1=value1&var4=value4

How to pass a JavaScript array as parameter to URL and catch it in PHP?

You are passing the data to php with fetch() intead of ajax, so the alternative of my first answer to do the same with the fetch() is:

var trafficFilterHolder = ["roadworks","snow","blocking"];
var trafficFilterHolderJoin = trafficFilterHolder.join(); // comma-separeted format => "roadworks,snow,blocking"

Now add the trafficFilterHolderJoin variable to the traffic query of the URL of your fetch(), like:

fetch('script.php?traffic=' + trafficFilterHolderJoin)

Then in your php script file you will convert the comma-separeted format to php array format using the explode function:

$traffic = explode(",", $_GET['traffic']);

Laravel: how to pass GET array to controller?

There is few ways to get data from GET.
Try something like that:

$ids = request('ids', []); // global helper, empty array as default

or:

$ids = $request->input('ids', []); // via injected instance of Request

More info you can get in documentation

Passing arrays as url parameter

There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:

$data = array(
1,
4,
'a' => 'b',
'c' => 'd'
);
$query = http_build_query(array('aParam' => $data));

will return

string(63) "aParam%5B0%5D=1&aParam%5B1%5D=4&aParam%5Ba%5D=b&aParam%5Bc%5D=d"

http_build_query() handles all the necessary escaping for you (%5B => [ and %5D => ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.



Related Topics



Leave a reply



Submit