I have a form that is taking user input and placing it into an array. Each form item has a label. The text in the labels corresponds to the items in the array $items.
if(isset($_POST['submit'])){
$items = array('Apple', 'Banana', 'Oranges', 'Grapes');
$amount = array();
foreach($_POST['item'] as $value){
$amount[]=($value);
}
$total =array_combine($items, $amount);
}
?>
<form method="post" action"">
<label>Apple</label><input type="text" name=item[]>
<label>Banana</label><input type="text" name=item[]>
<label>Orange</label><input type="text" name=item[]>
<label>Grapes</label><input type="text" name=item[]>
<input type="submit" name="submit" value="submit">
</form>
<?php
print_r($total);
?>
I combine both arrays and get the output as the numbers represent the amount of items. Array ( [Apple] => 12 [Banana] => 14 [Oranges] => 7 [Grapes] => 2 )
I want to be able to skip an item in the array if one of the forms is not filled in for example if oranges is missed out. I would like the array to output
Array ( [Apple] => 12 [Banana] => 14 [Grapes] => 2 )
I also tried imploding $total to output the array items
echo implode(",", $total);
However I only go the values from the $amount array and not both $items and $amount.