Last Updated: January 04, 2021
·
23.05K
· miguelos

Pass array through POST method

How to pass an array using POST method.

First use the function json_encode() to encode the array. Then use htmlspecialchars() to avoid errors caused by the quotes in json. Just like the following code:

echo '<input type="hidden" name="array" value="'.htmlspecialchars(json_encode($array)).'">';

To obtain the array do the inverse operation. First decode using htmlspecialcharsdecode() and then use jsondecode() to build the original array.

$array = json_decode(htmlspecialchars_decode($_POST['$array']));

More info:
http://stackoverflow.com/questions/4237090/how-to-pass-array-through-hidden-field