Submitting a multidimensional array via POST with php

Submitting a multidimensional array via POST with php

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

Stefan Bogdanescu

Founder & Senior Architect

Parsing Dynamic Forms for Multidimensional Arrays in PHP

Handling forms where the number of rows is unknown but the columns are fixed is a common pattern in dynamic applications, often used for data entry interfaces like bill of materials or inventory lists. You correctly identified that using HTML input names with numeric brackets—like topdiameter[0], bottomdiameter[1]—is the standard mechanism to signal an array structure to PHP. When the form is submitted via POST, PHP automatically populates these into a series of one-dimensional arrays.

However, your goal is to consolidate this data into a single multidimensional array for easier processing and email formatting. This requires iterating through the specific keys sent by the browser and reconstructing the row data programmatically.

In PHP 7 and later, you can leverage array_column combined with array_combine to achieve this efficiently, provided all your column fields share a consistent naming pattern. If your inputs are named topdiameter[0], bottomdiameter[0], topdiameter[1], and bottomdiameter[1], you can extract the row indices first:

$keys = array_keys($_POST);
// Filter out only the keys that contain '[' to isolate the array indices
$rowKeys = array_filter($keys, fn($key) => strpos($key, '[') !== false);

if (!empty($rowKeys)) {
    // Extract just the numeric index from keys like 'topdiameter[0]'
    $indices = array_map(function ($key) {
        return explode('[', $key)[1];
    }, $rowKeys);
    
    // Create a multidimensional array where each key is an index and value is another array
    $multiDimensionalArray = [];
    foreach ($indices as $index) {
        $rowData = [
            'top_diameter' => $_POST['topdiameter'][$index],
            'bottom_diameter' => $_POST['bottomdiameter'][$index],
            // Add other fields here like 'fabric', 'colour', 'quantity'
        ];
        $multiDimensionalArray[$index] = $rowData;
    }
}

This approach explicitly maps the raw POST data into a structured format. While this works well, it relies on you having access to the raw $_POST superglobal and manually parsing the keys. A more robust alternative in modern PHP is using array_column to pivot the data if you have consistent naming conventions across all rows.

// Assuming $_POST['topdiameter'] contains [0, 1, 2] and similar for others
$topDiameters = array_values(array_filter($_POST['topdiameter']));
$bottomDiameters = array_values(array_filter($_POST['bottomdiameter']));

$rows = [];
for ($i = 0; $i < count($topDiameters); $i++) {
    $rows[$i] = [
        'top_diameter' => $topDiameters[$i],
        'bottom_diameter' => $bottomDiameters[$i],
        // Include other fields similarly
    ];
}

This loop-based method is often safer because it avoids potential issues with sparse arrays or keys that might be filtered out by array_column if not handled carefully.

When sending this data via email, raw text is rarely the best choice for readability. You should pass the $multiDimensionalArray to a templating engine or use mail() with HTML headers to render a formatted table. Here is how you might prepare the data for an HTML email:

$htmlContent = '<table border="1" cellpadding="5">';
foreach ($rows as $row) {
    $htmlContent .= '<tr>';
    foreach ($row as $key => $value) {
        $htmlContent .= "<td><strong>$key</strong>: $value</td>";
    }
    $htmlContent .= '</tr>';
}
$htmlContent .= '</table>';

mail($clientEmail, 'Order Details', $htmlContent);

A critical pitfall to avoid is assuming that the POST data keys will always be sequential. If a user deletes a row via JavaScript before submission, PHP's $_POST might skip an index, leading to misaligned arrays if you rely solely on numeric indices without validation. Always validate that required fields exist for every detected row index before processing.

If you are building this application using the Laravel framework, the request() helper simplifies access to these inputs and provides better validation pipelines. You can define rules in your controller or form request class to ensure every row has valid data before constructing the final array. For complex scenarios involving dynamic rows, consider using a package like laravelcollective/html for email rendering or sticking to simple string interpolation if you need lightweight solutions.

Ultimately, converting parallel arrays into a multidimensional structure is about normalizing unstructured input into a predictable model. Whether you use manual loops or functional approaches like array_column, the key is ensuring your logic accounts for the dynamic nature of the form rows and cleans up the data before transmission.

Our network