PHP – How to send an array to another page?

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

The session has the advantage that the client doesn’t see it (therefore can’t tamper with it) and it’s faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.

Edit: a lot of answers are suggesting using name="input_name[]". This won’t work in the general case – it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.

Leave a Comment