How to get a form input array into a PHP array

How to get a form input array into a PHP array

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

Stefan Bogdanescu

Founder & Senior Architect

Pairing Form Arrays in PHP

When building dynamic forms where users can add multiple entries, such as adding several contact records via jQuery, you often end up with parallel arrays in your $_POST superglobal. A common mistake is assuming that simply iterating over one array will automatically align with the corresponding element in another array just because they share the same length.

In your specific case, you have inputs named name[] and email[]. When PHP processes the request, it correctly populates these as indexed arrays:

$_POST['name']   // ['name1', 'name2', 'name3']
$_POST['email']  // ['email1', 'email2', 'email3']

If you iterate through them separately using a simple foreach loop, you will indeed process every name and every email. However, this approach loses the association between the specific name and its corresponding email address. You are effectively printing "name1", then "name2", then "name3", followed by all emails in order. This happens because PHP does not maintain a hidden index linking the two separate loops; they just run sequentially on their respective arrays.

To achieve the desired output—where each name is paired with its specific email—you need to iterate over both arrays simultaneously. The most robust way to do this without relying on array_map for complex logic (which can be tricky when mixing strings and functions) is using a foreach loop that maintains the index or iterates over one array while accessing the parallel one by key.

Here is the corrected approach using standard PHP syntax:

$names = $_POST['name'];
$emails = $_POST['email'];

// Ensure arrays exist and have data before looping
if (!empty($names) && !empty($emails)) {
    // Iterate over the names array and access the corresponding email by index
    foreach ($names as $index => $name) {
        $email = $emails[$index];
        
        // Call your function with both paired values
        echo show_Names($name, $email);
    }
}

function show_Names($n, $m)
{
    return "The name is {$n} and email is {$m}, thank you";
}

Why array_map Can Be Tricky Here

You mentioned wanting to use array_map to achieve the pairing:

$c = array_map("show_Names", $a, $b);

Technically, passing multiple arrays to array_map works in PHP 5.3+ and will pass corresponding elements from each array as arguments to the callback function. However, there is a significant pitfall: your function show_Names expects two string arguments, but if you pass it directly as a string "show_Names", PHP interprets this as passing the string itself as the first argument in some contexts depending on how the call is wrapped, or more commonly, array_map requires the callback to be a valid callable. If you are using PHP 5.x, you must ensure the function is defined globally and accessible. In modern PHP (7.4+), you would typically pass the function as an array:

$c = array_map('show_Names', $names, $emails);

However, this still assumes that both arrays are perfectly aligned. If a user adds a name but forgets to add an email for that specific entry (or if one input is left blank), the index alignment breaks. If $names has 3 items and $emails has 2, array_map will only process 2 pairs, silently ignoring the third name.

Handling Empty Inputs and Security

A critical trade-off in dynamic forms is handling empty inputs. If a user submits a form where they add three rows but leave the email blank on the third row, your parallel array logic might still pair them based on index count. You must decide whether to treat an empty string as valid data or skip it.

Furthermore, always sanitize and validate input before processing. Never trust that the number of name inputs exactly matches the number of email inputs unless you explicitly enforce this in your frontend validation or backend checks. If you are using Laravel, you can leverage its request handling to ensure strict type checking or collection mapping:

// Example logic within a Laravel Controller
$names = request('name');
$emails = request('email');

foreach ($names as $i => $name) {
    // Validate that an email exists at this index
    if (isset($emails[$i])) {
        echo show_Names($name, $emails[$i]);
    } else {
        // Handle the case where email is missing for a specific name
        continue; 
    }
}

Conclusion on Parallel Arrays

The core takeaway is that $_POST['name'] and $_POST['email'] are independent arrays. They only appear linked because you generated them in parallel HTML inputs. To process them as pairs, you must explicitly iterate using an index ($index => $value) to fetch the corresponding element from the second array. This method offers better control over validation errors and edge cases compared to blindly trusting array_map without checking array lengths first.

Our network