$idArray = array(1,2,3,4);
can 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 !!
You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference).
php // create the 'scores' array $scores = array(9,7,112,89,633,309); // create the 'average' function function average($array){ // set 'total' to 0 $total = 0; foreach($array as $value){ // adds the value of each item in the array, one by one $total += $value; } // calculate the average and return the result return $ ...
Now that we know how to pass the variables in the URL, we're going to get it in PHP using $_GET. $_GET is a built-in variable of PHP which is an array that holds the variable that we get from the URL.
You can make use of serialize() and urlencode PHP built-in function to pass an array as URL param. The serialize() function will return a sequence of bits for the input given and the urlencode will again encode the values as well the special characters available in it.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With