How to Pass an Array via $_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.

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.

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 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)

Codeigniter: Pass array of arrays from view to controller

I found the solution to my problem. Here is what worked for me.
I was passing my array of arrays as a string value in a hidden input field using

json_encode($array)

but the problem was that my keys was double quoted and as a result the

value="<?php echo json_encode($array);?>"

was breaking down...

The solution was to escape the characters, so I had to replace the above line with

value="<?php echo htmlspecialchars(json_encode($array));?>"

And in the controller I had to get my array from json with the following lines

$dataJson = $this->input->post('array');
$dataArray = json_decode(htmlspecialchars_decode($dataJson), true);

Thanks everyone for the answers!



Related Topics



Leave a reply



Submit