PHP - How to send an array to another page?

PHP - How to send an array to another page?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Printing an Array into an Input Field Breaks Your Logic

Attempting to pass an array between pages by embedding print_r() output directly into an <input> field is a common stumbling block for developers new to data serialization. The fundamental issue isn't that you can't move data; it's that you are treating structured data as if it were a simple string. When you execute <?php print_r($array_name); ?> inside an HTML attribute, PHP outputs a human-readable representation of the array—something like Array ( [0] => item1 [1] => item2 ).

When this string reaches Page 2 via $_POST['input_name'], it arrives exactly as that messy string. Assigning it to $passed_array does not magically reconstruct the data structure. To make $passed_array act like an array again, you must parse that specific string back into a PHP variable. The standard approach is using unserialize(), but this requires sending the data in serialized format from the start.

Serialization vs. Direct String Output

The most robust solution without relying on sessions or cookies is to use PHP's native serialization functions: serialize() and unserialize(). These functions convert an array into a single string that preserves the internal structure, types, and nesting of the data.

On Page 1, instead of printing the array directly, you serialize it before echoing it into the hidden input field:

<?php
$data = ['username' => 'john', 'roles' => ['admin', 'user']];

// Serialize the array to a string
$serialized_data = serialize($data);
?>

<input type="hidden" name="input_name" value="<?php echo htmlspecialchars($serialized_data); ?>" />

Notice the use of htmlspecialchars(). Even though we are serializing data, wrapping the output in this function is a critical security practice to prevent breaking out of the HTML context if the serialized string contains special characters like quotes or ampersands.

On Page 2, you retrieve the value and reverse the process:

<?php
$raw_value = $_POST['input_name'];

if ($raw_value) {
    // Unserialize the string back to an array
    $passed_array = unserialize($raw_value);
} else {
    $passed_array = [];
}

// Now $passed_array is a real array with all original data intact
print_r($passed_array); 
?>

This method strictly adheres to your constraint of avoiding sessions and cookies. The data travels entirely within the $_POST payload, hidden from direct view in the browser source until the page processes it.

Handling JSON as an Alternative

While serialize() is built into PHP, many developers prefer JSON for inter-page communication because it uses a universal format and is easier to debug in browser developer tools. You can achieve the same result using json_encode() and json_decode().

On Page 1:

<?php
$data = ['username' => 'john', 'roles' => ['admin', 'user']];
$json_data = json_encode($data);
?>

<input type="hidden" name="input_name" value="<?php echo htmlspecialchars($json_data); ?>" />

On Page 2:

<?php
$raw_value = $_POST['input_name'];
if ($raw_value) {
    $passed_array = json_decode($raw_value, true); 
}
// Note: json_decode returns an object by default. Passing 'true' converts it to an associative array.
print_r($passed_array);
?>

The Danger of print_r and var_dump

You might be tempted to stick with your original approach using print_r() because the output looks like it represents the data structure. However, relying on this is fragile. If your array keys are numeric strings or if you have nested arrays, print_r changes its output format based on configuration settings like output_html. This inconsistency makes unserialize() incompatible with print_r() output, leading to errors when trying to restore the data.

Furthermore, print_r includes a lot of noise, such as the literal text "Array" or indentation characters. Parsing this manually using eval() is possible but highly discouraged due to severe security risks involving code injection. Serialization functions are safe because they only accept valid PHP data structures as input; passing a malformed string to unserialize() simply returns false rather than executing arbitrary code.

Conclusion on Data Transfer

To move complex data between pages without state management like sessions, serialization is the correct pattern. It ensures that the data structure remains intact during transit and can be reliably reconstructed on the receiving end. Whether you choose serialize for pure PHP environments or json_encode for broader compatibility, the key takeaway is always to encode data into a string before placing it in an input field, and decode it immediately upon retrieval using the matching function.

Our network