Multiple inputs with same name through POST in php

Multiple inputs with same name through POST in php

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Multiple Inputs with the Same Name in PHP

Standard HTML forms rely on unique name attributes for inputs to map directly to keys in the $_POST superglobal. However, there is a specific architectural pattern where you need to collect a series of items—like an indefinite list of physical addresses—and process them as a collection rather than individual distinct fields. The short answer is yes, PHP can handle this, but it requires a deliberate naming convention on your HTML side and specific handling logic on the server.

To access multiple inputs with the same name in PHP, you must append an index to the name attribute of each subsequent input element within the form. If your goal is to access these values as a zero-indexed array (e.g., $_POST['xyz'][0]), every input field targeting that key must increment its suffix sequentially.

Consider a scenario where you are building a shipping address wizard. You have fields for street, city, and state, but you want the user to add five different locations before submitting. If you simply named them all address, PHP would overwrite the previous value with every new input, leaving you with only the last entry in $_POST['address']. To preserve the data as an array, the HTML structure needs to look like this:

<form method="post">
    <!-- First entry -->
    <input type="text" name="address[]" value="" placeholder="Street 1">
    
    <!-- Second entry -->
    <input type="text" name="address[]" value="" placeholder="Street 2">
    
    <!-- Third entry -->
    <input type="text" name="address[]" value="" placeholder="Street 3">
</form>

Notice the syntax name="address[]". The brackets indicate to both the browser and the server that this field should be treated as an array. When the form submits, PHP automatically aggregates these inputs into a single indexed array accessible via $_POST['address']. You can then iterate through them using a standard foreach loop or access specific items directly by their index.

There is a subtle distinction between the syntax used in your mental model versus the actual implementation. Your question mentioned accessing data like $_POST['xyz'][0]. While this works perfectly fine for reading, generating the HTML requires the [] suffix on the input tag itself. If you omit the brackets in the HTML (e.g., name="xyz"), PHP will treat the subsequent inputs as string overwrites rather than array elements, and accessing them via an index like [0] will result in a notice or undefined index error depending on your configuration.

Beyond standard arrays, there is another common pattern where you need to handle multiple items with named keys instead of numeric indices. For instance, if you have three address blocks and want to distinguish them by name rather than order (Address 1, Address 2, Address 3), you can use the name attribute followed by an explicit key:

<input type="text" name="address[street][]" value="">
<input type="text" name="address[city][]" value="">

This approach creates a multidimensional array structure where $_POST['address']['street'] returns the list of street addresses. This is particularly useful when dealing with complex objects or when you need to validate specific sub-fields independently later in your processing logic.

When integrating this into a modern stack, frameworks often abstract away the raw $_POST access to provide cleaner data structures. For example, Laravel provides robust methods for handling form requests and validation, ensuring that array inputs are processed safely without exposing raw superglobals directly in your controller logic. If you are working on a larger application where maintaining clean separation of concerns is critical, exploring resources like LaravelCompany can offer insights into best practices for managing complex form submissions within that ecosystem.

The primary pitfall to avoid is relying solely on the order of inputs in the DOM without explicit naming conventions. Browsers do not guarantee input order in the event payload; they rely entirely on the name attributes provided. Therefore, the sequential indexing method (address[0], address[1]) or the array suffix method (address[]) must be strictly enforced in the HTML generation to ensure server-side consistency.

Our network