Click to See Complete Forum and Search --> : Post 2 arrays and combine them


rogernem
January 19th, 2011, 07:44 AM
I have a form like the following


<form action="" method="post" name="fcds">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>TYPE</td>
<td>PHONE</td>
</tr>
<tr>
<td>
<select name="myCheckbox[]" >
<option value="">Selecione</option>
<option value="1">Mobile</option>
<option value="2">Job</option>
<option value="3">Other</option>
<option value="4" selected="selected">Home</option>
</select>
</td>
<td><input type="text" name="fone[]" /></td>
</tr>
<tr>
<td><select name="myCheckbox[]" >
<option value="">Selecione</option>
<option value="1" selected="selected">Mobile</option>
<option value="2">Job</option>
<option value="3">Other</option>
<option value="4">Home</option>
</select></td>
<td><input type="text" name="fone[]" /></td>
</tr>
<tr>
<td><select name="myCheckbox[]" >
<option value="">Selecione</option>
<option value="1">Mobile</option>
<option value="2" selected="selected">Job</option>
<option value="3">Other</option>
<option value="4">Home</option>
</select></td>
<td><input type="text" name="fone[]" /></td>
</tr>
<tr>
<td><select name="myCheckbox[]" >
<option value="">Selecione</option>
<option value="1">Mobile</option>
<option value="2">Job</option>
<option value="3" selected="selected">Other</option>
<option value="4">Home</option>
</select></td>
<td><input type="text" name="fone[]" /></td>
</tr>
</table>
<input value="ok" type="submit" />
</form>


I want the final result to be an array with only the not empty values posted like this:
result[0] = type,fonevalue -> 1, 8765-8976
result[1] = 2, 543-3654
result[2] = 3, 342-3456

I´ve been using

foreach($_POST['myCheckbox'] as $type) {
if($type<>'') echo $type;
}
foreach($_POST['fone'] as $phone) {
if($phone<>'') echo $phone;
}


But that just gives me the arrays separate.
I want to combine them so I´d have the type together with the corresponding phone value.

PeejAvery
January 19th, 2011, 10:17 AM
Maybe I'm misunderstanding, but your code is so close...

$checkboxes = array();
foreach ($_POST['myCheckbox'] as $index => $checkbox) {
$checkboxes[$index] = $checkbox;
}

print_r($checkboxes);

rogernem
January 19th, 2011, 11:20 AM
that just gives me Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 ) but where are the phone values?
See?

IŽd need something like this:

Array ( [0] => 4, 8765-8976 [1] => 1, 333-2222 [2] => 2, 456-0987 )

PeejAvery
January 19th, 2011, 11:28 AM
You have the $index...just add them in there.

rogernem
January 20th, 2011, 07:15 AM
This was what I did:


$checkboxes = array();
foreach ($_POST['myCheckbox'] as $index => $checkbox) {
$checkboxes[$index] = $checkbox.",".$_POST['fone'][$index];
}

print_r($checkboxes);


Array ( [0] => 4,(33) 333 [1] => 1, [2] => 2,(11) 1111 [3] => 3, )

I was still getting empty ones, so to avoid avoid them I did:

$checkboxes = array();
foreach ($_POST['myCheckbox'] as $index => $checkbox) {
if($_POST['fone'][$index]!='') $checkboxes[$index] = $checkbox.",".$_POST['fone'][$index];
}

print_r($checkboxes);


Array ( [0] => 4,(33) 333 [2] => 2,(11) 1111 )

Instead of having [0] and [2] I´d like to have [0] and [1]. How can I do that?

PeejAvery
January 20th, 2011, 07:33 AM
Sort the array or add a fake index scheme that only gets incremented when the value is not empty.

And, you should be using !empty() instead of != ''.

rogernem
January 20th, 2011, 12:56 PM
Thanks.

This is my final code

$checkboxes = array();
$i=0;
foreach ($_POST['myCheckbox'] as $index => $checkbox) {
if(!empty($_POST['fone'][$index])){
$checkboxes[$i] = $checkbox.",".$_POST['fone'][$index];
$i++;
}
}


Array ( [0] => 4,(33) 333 [1] => 2,(11) 1111 )