Problem:
You have a form that generates multiple input fields with arrays as their names (e.g., name[] and email[]). When you retrieve these inputs in PHP, you end up with a concatenated string rather than individual arrays. How can you convert these inputs into proper arrays?
Solution:
To convert the form input arrays into individual arrays in PHP:
Implementation:
$name = $_POST['name']; $email = $_POST['account']; foreach ($name as $key => $n) { // Get the corresponding email address using the key $e = $email[$key]; // Print the values or process them as needed echo "The name is $n and email is $e, thank you\n"; }
Example:
Consider the following form:
<input type="text" name="name[]" /> <input type="text" name="email[]" /> <input type="text" name="name[]" /> <input type="text" name="email[]" /> <input type="text" name="name[]" /> <input type="text" name="email[]" />
When you submit this form, the $_POST array will contain the following:
$_POST = [ 'name' => ['name1', 'name2', 'name3'], 'email' => ['email1', 'email2', 'email3'], ];
Using the above solution, you can easily access and process the form inputs:
foreach ($_POST['name'] as $key => $n) { $e = $_POST['email'][$key]; echo "The name is $n and email is $e, thank you\n"; }
Output:
The name is name1 and email is email1, thank you The name is name2 and email is email2, thank you The name is name3 and email is email3, thank you
The above is the detailed content of How to Properly Access and Process Form Input Arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!