0

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.

lukenjohnson
  • 113
  • 1
  • 10

2 Answers2

1

Iterate over one of arrays with key=>value and grab same key from other arrays:

foreach ($_POST['q_number'] as $key => $value) {
    echo $value, ' ', $_POST['q_title'][$key], ' ', $_POST['q_desc'][$key]; // etc
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Two Loops

Without having to know the name of the keys you can do this with two loops

<?php
$data = [];
$data['q_number'] = [];
$data['q_number'][] = 1;
$data['q_number'][] = 2;
$data['q_number'][] = 3;

$data['q_answer'][] = "a";
$data['q_answer'][] = "b";
$data['q_answer'][] = "c";


$data['q_required'][] = true;
$data['q_required'][] = false;
$data['q_required'][] = false;

$grouped = [];

foreach($data as $key =>$valueset){
  $counter = 0;
  foreach($valueset as $key2 => $value){
    $grouped[$counter][$key] = $value;
    $counter +=1;
  }

}

var_dump($grouped);
?>
Robert I
  • 1,509
  • 2
  • 11
  • 18