Issue pushing item to the end of a PHP array?

754

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.

28

Answer

Solution:

I'm not going to rewrite all your code, but:

$to_array_count = count($to_array);

$to = $to_array["0"];

for ($v = 1; $v < $to_count; $v++) {

   $to = $to . ',' . $to_array[$v];

}

should probably just be:

$to = implode(',', $to_array);

Not really sure why you wouldn't use aforeach loop anyways, if you were going to do it the looney way.

People are also looking for solutions to the problem: php - EDIT/UPDATE comma separated values into mysql rows and add new

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.