<p>At the moment my code looks like this:</p>

<pre><code># Assign values for saving to the db
$data = array(
    'table_of_contents' =&gt; $_POST['table_of_contents'],
    'length' =&gt; $_POST['length']
);

# Check for fields that may not be set
if ( isset($_POST['lossless_copy']) )
{
   $data = array(
       'lossless_copy' =&gt; $_POST['lossless_copy']
    );
}

// etc.
</code></pre>

<p>This would lead to endless if statements though... Even with the ternary syntax it's still messy. Is there a better way?</p>

```
```
foreach ($_POST as $key => $value) {
  $data[$key] = $value;
}
```
```
remember to sanitize your $_POST values!
edit
: if you're looking to match up optional $_POST values with fields that may or may not exist in your table, you could do something like this (i'm assuming you're using mysql):
```
```
$fields = array();
$table  = 'Current_Table';

// we are not using mysql_list_fields() as it is deprecated
$query  = "SHOW COLUMNS from {$table}";
$result = mysql_query($query);
while ($get = mysql_fetch_object($result) ) {
  $fields[] = $get->Field;
}

foreach($fields as $field) {
  if (isset($_POST[$field]) ) {
    $data[$field] = $_POST[$field];
  }
}
```
```