Issue pushing item to the end of a PHP array?
I have the following code...
if ($email_selection == "primary") {
$default_select = "<option value=''>Select Email</option>";
$select_1 = "selected";
$to_array[] = $profile_primary_email;
} elseif ($email_selection == "secondary") {
$default_select = "<option value=''>Select Email</option>";
$select_2 = "selected";
$to_array[] = $profile_primary_email;
} elseif ($email_selection == "both") {
$default_select = "<option value=''>Select Email</option>";
$select_3 = "selected";
$to_array[] = $profile_primary_email . ',' . $profile_secondary_email;
}
if ($manual_email != "") {
$to_array[] = $manual_email;
}
$to_array_count = count($to_array);
$to = $to_array["0"];
for ($v = 1; $v < $to_count; $v++) {
$to = $to . ',' . $to_array[$v];
}
The function of this code is to get the value of a select input, and based on the select input's value push an email address to the end of$to_array
. Then the script makes a string of each email seperated by a comma which will later be used as the$to
in the PHPmail()
function.
For some reason, the select field contains any value, the email address contained in$manual_email
is not appended to the array. If there is no value to the select field, however,$manual_email
is appended to the empty array normally.
Answer
Solution:
I'm not going to rewrite all your code, but:
should probably just be:
Not really sure why you wouldn't use a
foreach
loop anyways, if you were going to do it the looney way.