Passing an array using an HTML form hidden element

Passing an array using an HTML form hidden element

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Trap of Embedding Arrays in Form Fields

The core issue in your current implementation stems from a fundamental misunderstanding of how HTML forms transmit data versus how PHP represents arrays. When you attempt to embed a PHP array directly into an HTML attribute using <?php echo $postvalue; ?>, the browser does not see an array object. Instead, it sees whatever string result that specific line of code produces at runtime.

In your example, $postvalue is an indexed array containing three strings. When echoed without modification, PHP converts this array into a JSON-like representation (depending on version and settings) or simply prints the raw elements separated by commas. The resulting HTML input field will look something like this:

<input type="hidden" name="result" value="[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]">

Notice the surrounding brackets and the escaped quotes. When your form is submitted, the server receives result as a single string containing that entire sequence. If you try to access this via $_POST['result'], you are indeed getting that string. You cannot simply cast it back to an array using standard PHP operators like explode() because the syntax [ "a", "b", "c" ] is not valid for simple splitting, and relying on specific output formats of echo is brittle.

The standard approach to passing multiple values from a client-side form is to treat each element as a separate input field sharing the same name. This creates an array structure automatically upon submission. You can achieve this by looping through your data and generating inputs dynamically:

<?php
$postvalue = ["a", "b", "c"];

foreach ($postvalue as $item) {
    echo '<input type="hidden" name="result[]" value="' . htmlspecialchars($item) . '">';
}
?>

By adding the square brackets [] to the input name, you signal to PHP that these inputs should be aggregated into an array. Upon submission, $_POST['result'] will correctly contain an array of strings: ["a", "b", "c"]. This method is robust because it relies on the native parsing logic of the HTTP request rather than string manipulation hacks.

However, there are scenarios where you genuinely need to send a serialized payload, such as when dealing with JSON data that must be parsed later. If your application requires sending complex objects or nested arrays in a single hidden field, you must serialize the data before assigning it to the value attribute. The most common serialization function is json_encode().

<?php
$postvalue = ["a", "b", "c"];
echo '<input type="hidden" name="result" value="' . htmlspecialchars(json_encode($postvalue)) . '">';
?>

This approach generates a valid JSON string inside the hidden field. When retrieved in PHP, you must use json_decode() and cast the result to an array:

$rawValue = $_POST['result'];
$resultArray = json_decode($rawValue, true);

Using true as the second argument ensures you get an associative or indexed array rather than a stdClass object. This pattern is essential when the data structure is complex and cannot be represented by simple parallel inputs.

Security considerations are paramount in this context. When outputting any variable into an HTML attribute, specifically the value attribute of an input tag, you must sanitize the content to prevent Cross-Site Scripting (XSS). If your array contains user-generated data or strings with quotes, simply echoing them directly can break the HTML structure or inject malicious scripts. Always wrap the value in htmlspecialchars(), which converts special characters like <, >, and & into their HTML entity equivalents. This ensures that even if a string inside your array looks like an HTML tag, it is rendered as text within the form field rather than executed code.

While modern frameworks often abstract these details away, understanding the underlying mechanism of form submission helps when debugging issues or working with legacy systems. If you are using Laravel, the framework handles request()->input() and request->get() methods which automatically parse multi-dimensional arrays correctly if the input fields follow the name[] convention or are properly serialized. For more complex needs in Laravel applications, resources like https://laravelcompany.com can provide guidance on handling request payloads with their specific helpers.

Our network