I wish to store an arbitrary number of answers to a question on a form in a single field (json).
So the inputs may be something like:
Your Jobs:
I know I can call the input <input name="job_title[]">
and these would all be listed as an array under job_title
. But is there anyway to have all three inputs listed under a keyed array just by naming the inputs in a specific way, something like:
[jobs => [['job_title' => 'xyz', 'start_date` => 'xyz', 'send_date` => 'xyz'], ....]
Or would I need to build this manually in php with loops?
Do you want to create an array based on the Posted input value? Just use the foreach loop with the $_POST
variable like
<?php
if (isset($_POST)) {
foreach ($_POST as $key => $value) { // for every post value
$array[] = array( // we create a array based on the name and the value
$key => $value
);
}
var_dump($array); // dump the array
}
?>
<form method="POST">
<input type="text" name="job title" value="xyz">
<input type="text" name="start date" value="xyz">
<input type="text" name="send date" value="xyz">
<!-- any other input -->
<button type="submit">sd</button>
</form>
The var_dump($array)
will give you
array(2) { ["job_title"]=> string(3) "xyz" ["start_date"]=> string(3) "xyz" } array(1) { ["send_date"]=> string(3) "xyz" }
To create JSON from the array just do
echo json_encode($array)
If you are only wanting a single subset group, you can specify the key to use in your input list
<input name="name" value="John Doe">
<input name="jobs[job_title]" value="xyz">
<input name="jobs[start_date]" value="xyz">
<input name="jobs[send_date]" value="xyz">
Results:
$_POST => [
'name' => 'John Doe'
'jobs' ['job_title' => 'xyz', 'start_date' => 'xyz', 'send_date' => 'xyz'],
//...
];
If you want to accept more than one job, depending on how you would like your data to be associated, it is recommended to use an explicitly defined grouping index.
To associate the values sequentially, similarly to how you would receive from a database result set, you can use a simple incremental variable for your data set groups.
<?php $i = 0; ?>
<input name="jobs[<?php echo $i; ?>][job_title]" value="xyz">
<input name="jobs[<?php echo $i; ?>][start_date]" value="xyz">
<input name="jobs[<?php echo $i; ?>][send_date]" value="xyz">
<?php $i++; ?>
<input name="jobs[<?php echo $i; ?>][job_title]" value="xyz">
<input name="jobs[<?php echo $i; ?>][start_date]" value="xyz">
<input name="jobs[<?php echo $i; ?>][send_date]" value="xyz">
<?php $i++; ?>
...
Results: $_POST['jobs'][$group][$field]
$_POST => [
'jobs' => [
0 => ['job_title' => 'xyz', 'start_date' => 'xyz', 'send_date' => 'xyz'],
1 => ['job_title' => 'xyz', 'start_date' => 'xyz', 'send_date' => 'xyz'],
//...
],
];
Excluding the input grouping value
$i
, likename="jobs[][field]"
will result in PHP automatically incrementing the$_POST
array index of the jobs.It is the equivalent of using
$jobs[] = ['field' => 'value']
$_POST => [ 'jobs' => [ 0 => ['job_title' => 'xyz', ], 1 => ['start_date' => 'xyz'], 2 => ['send_date' => 'xyz'] ] ];
Alternatively you can use a database identifier column value when iterating over the results, which is beneficial when performing updates to existing records.
<?php
$results = [
['id' => 1, 'job_title' => 'abc', 'start_date' => 'abc', 'send_date' => 'abc'],
['id' => 2, 'job_title' => 'xyz', 'start_date' => 'xyz', 'send_date' => 'xyz'],
];
foreach ($results as $row) { ?>
<input name="jobs[<?php echo $row['id']; ?>][job_title]" value="<?php echo $row['job_title']; ?>">
<input name="jobs[<?php echo $row['id']; ?>][start_date]" value="<?php echo $row['start_date']; ?>">
<input name="jobs[<?php echo $row['id']; ?>][send_date]" value="<?php echo $row['send_date']; ?>">
<?php } ?>