|
-
January 19th, 2011, 08:44 AM
#1
Post 2 arrays and combine them
I have a form like the following
Code:
<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
Code:
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.
-
January 19th, 2011, 11:17 AM
#2
Re: Post 2 arrays and combine them
Maybe I'm misunderstanding, but your code is so close...
PHP Code:
$checkboxes = array(); foreach ($_POST['myCheckbox'] as $index => $checkbox) { $checkboxes[$index] = $checkbox; }
print_r($checkboxes);
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 19th, 2011, 12:20 PM
#3
Re: Post 2 arrays and combine them
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 )
-
January 19th, 2011, 12:28 PM
#4
Re: Post 2 arrays and combine them
You have the $index...just add them in there.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 20th, 2011, 08:15 AM
#5
Re: Post 2 arrays and combine them
This was what I did:
Code:
$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:
Code:
$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?
Last edited by rogernem; January 20th, 2011 at 08:32 AM.
-
January 20th, 2011, 08:33 AM
#6
Re: Post 2 arrays and combine them
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 != ''.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 20th, 2011, 01:56 PM
#7
Re: Post 2 arrays and combine them
Thanks.
This is my final code
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 )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|