I have a form in which can be a few lines of inputs the same a few times, like this,
<input type="text" name="company[]"><input type="text" name="type[]">
<input type="text" name="company[]"><input type="text" name="type[]">
<input type="text" name="company[]"><input type="text" name="type[]">
Now I have to input these fields to the database, so I looped through the the input fields and it works fine.
But I have one problem: sometimes the fields in the loop can be empty. I.e. the company will have a value but not the type. So how can I make so that the loop should output the empty value in a key like this:
Array(
company => array(
[0] => string0
[1] => string1
[2] => string2
)
type => array(
[0] =>
[1] => string1
[2] => string2
)
)
So you can see that first key of type is empty, so how can I achieve that,
I'm trying to do this but no result,
$postFields = array('company', 'type');
$postArray = array();
foreach($postFields as $postVal){
if($postVal == ''){
$postArray[$postVal] = '';
}
else {
$postArray[$postVal] = $_POST[$postVal];
}
}
Appreciate any help,
value=""to each tag not do it for you?type="text", an empty value will be in the$_POSTarray... I'm struggling to see the problem / desired output here...name's likename="company[1]"etc. to keep company and type values related to each other.