0

I have the following form, that passes an array of first names, last names, and ages.

<form method="post" action="form.php">
    <label for="first_name">First Name:</label>
    <input type="text" name="first_name[]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="last_name[]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="age[]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="first_name[]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="last_name[]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="age[]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="first_name[]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="last_name[]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="age[]" id="age" />
    <br />
    <input type="submit" name="save" id="save" value="Save" />
</form>

When the form us submitted, I recieve the following array structures in PHP.

Array
(
    [first_name] => Array
        (
            [0] => Dave
            [1] => Lisa
            [2] => Support
        )

    [last_name] => Array
        (
            [0] => Lucas
            [1] => Newman
            [2] => Services
        )

    [age] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [save] => Save
)

How do I turn the above array into the following structure so that a loop can be created to add one record (first_name, last_name, age) at a time to a MySQL db/table.

e.g. first_name[0], last_name[0], and age[0], is one record.

I need the array to look similar to the following, in order to loop the data inserts.

Array
(
    [0] => Array
        (
            [first_name] => Steve
            [last_name] => Lucas
            [age] => 12
        )

    [1] => Array
        (
            [first_name] => Lisa
            [last_name] => Newman
            [age] => 44
        )

    [2] => Array
        (
            [first_name] => Owen
            [last_name] => McDowell
            [age] => 36
        )

    [save] => Save
)
0

2 Answers 2

1
$count = count ($array_in);

$array_out = array ();

for ($i = 0; $i < $count; $i++)
{
    $array_out[] = array
    (
        'first_name' => $array_in['first_name'][$i],
        'last_name' => $array_in['last_name'][$i],
        'age' => $array_in['age'][$i]               
    );

}

print_r ($array_out);
Sign up to request clarification or add additional context in comments.

Comments

0

Personally, I think you should change the form (if possible) so it outputs the array you actually want.

Currently you're outputting something you don't want only to change it to something you want, when you could just output what you wanted in the first place. This means less code, so is neater and faster, easier to maintain, etc.

You could change your HTML form to this..
I've just changed the name="" of each input so each form is added to the same array, but each form (ie each first, last, age) has its own sub array:

 <form method="post" action="form.php">
    <label for="first_name">First Name:</label>
    <input type="text" name="array[0][first_name]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="array[0][last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="array[0][age]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="array[1][first_name1]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="array[1][last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="array[1][age]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="array[2][first_name]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="array[2][last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="array[2][age]" id="age" />
    <br />
    <input type="submit" name="save" id="save" value="Save" />
</form>

Which would give you this:

Array
(
  [array] => Array 
    ( 
      [0] => Array
        (
            [first_name] => Steve
            [last_name] => Lucas
            [age] => 12
        )

      [1] => Array
        (
            [first_name] => Lisa
            [last_name] => Newman
            [age] => 44
        )

      [2] => Array
        (
            [first_name] => Owen
            [last_name] => McDowell
            [age] => 36
        )

      [save] => Save
   )
)

You could easily loop through those when inserting into your database.



To do exactly what you wanted:

Array
(
    [0] => Array
        (
            [first_name] => Steve
            [last_name] => Lucas
            [age] => 12
        )
//etc

you'd have to set the array name as numerical so you set the index of each sub array as numerical as you required, such as:

 <form method="post" action="form.php">
    <label for="first_name">First Name:</label>
    <input type="text" name="0[first_name]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="0[last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="0[age]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="1[first_name]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="1[last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="1[age]" id="age" />
    <br />
    <br />
    <br />
    <label for="first_name">First Name:</label>
    <input type="text" name="2[first_name]" id="first_name" />
    <br />
    <label for="last_name">Last Name:</label>
    <input type="text" name="2[last_name]" id="last_nametext" />
    <br />
    <label for="age">Age:</label>
    <input type="text" name="2[age]" id="age" />
    <br />
    <input type="submit" name="save" id="save" value="Save" />
</form>

I don't think this is a good idea.

2 Comments

Why don't you think this is a good idea? How would you do this? - I'm keen to find out what different method there are, and which ones are best out of all of them?
I would do it as per my first form code. It sets the array and sub arrays as per what I quoted. This way you create the array structure as you want it, and so no need to change the array later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.