2015-05-29 181 views
0

我有以下代码...将项目推送到PHP数组的末尾?

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]; 

} 

这段代码的作用是让一个选择输入的值,并基于选择输入的值推一个电子邮件地址的$to_array末。然后脚本将每个电子邮件的字符串用逗号分隔,稍后将用作PHP mail()函数中的$to

由于某些原因,选择字段包含任何值,$manual_email中包含的电子邮件地址不会附加到该数组。但是,如果选择字段没有值,则通常将$manual_email附加到空数组。

+0

'$ to_array [0]'和'$ to_array ['0']'在PHP中不一样。 – PHPglue

+0

删除或添加引号不会更改脚本的功能。我一直使用它们互换,并且很好。无论如何,我也有同样的问题。 – Allenph

+0

只是为了澄清最后一段,如果'$ to_array'包含一些数据,你是否在说'$ manual_email'没有被追加? – khuderm

回答

1

我不会重写所有的代码,但是:

$to_array_count = count($to_array); 

$to = $to_array["0"]; 

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

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

} 

应该可能只是:

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

真的不知道为什么你会不使用foreach循环不管怎么说,如果你打算这样做的方式。

+0

你说得对。这种方式效率低下,您的解决方案运行良好。谢谢。 – Allenph

相关问题