I am developing a form-builder to allow admins to create custom forms. I use a jQuery script to insert new HTML blocks into the page. Each HTML block contains a series of form fields which are appended with [] to be processed as arrays. (E.g., question_number[])
I am trying to find a way to group array values together according to array key number. E.g., group together q_number[3], q_label[3], q_desc[3], q_format[3], etc... Keys from different arrays, grouped together by array key number.
HTML
Here's the basic HTML outputted by my jQuery script so you can see the out-going variables:
<input name="q_number[]" type="text" class="q-number" placeholder="E.g., 148">
<select name="q_format[]" class="q-format">
<option value="text">Simple text field</option><option value="number">Number field</option>
<option value="textarea">Paragraph textbox</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio - choose from a list</option>
<option value="select">Dropdown select</option>
</select>
<input name="q_title[]" type="text" class="q-title" placeholder="E.g., Types of services held in 2016">
<textarea name="q_desc[]" class="q-desc" placeholder="Explanation to clarify the question."></textarea>
<textarea name="options[]" class="q-options" placeholder="option 1, option 2, option 3, etc."></textarea>
<input type="checkbox" checked="checked" name="required[]" value="1" class="q-required">
Array data
Here is what the resulting arrays look like when the form is processed with 3 questions added to the form:
[q_number] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[q_format] => Array
(
[0] => number
[1] => radio
[2] => radio
)
[q_title] => Array
(
[0] => Number of baptisms this year
[1] => Which service do you like best?
[2] => Which demographic is increasing most quickly?
)
[q_desc] => Array
(
[0] =>
[1] =>
[2] =>
)
[options] => Array
(
[0] =>
[1] => Holy Eucharist, Baptism, Confirmation, Wedding, Funeral
[2] => Infants, Children, Youth
)
[required] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
As you can see, the values of each array coordinate by key number.
I am looking for a way to do this which doesn't requiring knowing the number of keys in an array. This is for a nation-wide system, and each diocese can create its own forms, so I need a solution that can match array keys dynamically. (Once I have grouped the keys together, I will create one database record for each set of 'question' data so that I can generate the form that users will complete.)
It's easy enough to create a multidimensional array out of these -- but I don't know how to bring all the like-numbered keys together. Is there a way to do this with multidimensional arrays? Or is there another avenue I should be pursuing?
Thanks very much for your help.