我有以下的PHP摘录代码:有效的,和非冗余PHP代码
foreach($afb_replacements as $afb_to_replace => $afb_replacement) {
$sender_subject = str_replace($afb_to_replace, $afb_replacement, $sender_subject);
$ar_subject = str_replace($afb_to_replace, $afb_replacement, $ar_subject);
$final_message = str_replace($afb_to_replace, $afb_replacement, $final_message);
$final_message_text = str_replace($afb_to_replace, $afb_replacement, $final_message_text);
$ar_message = str_replace($afb_to_replace, $afb_replacement, $ar_message);
$ar_message_text = str_replace($afb_to_replace, $afb_replacement, $ar_message_text);
}
所有6个变量以同样的方式被替换(同文同在$ afb_to_replace所有变量相同的替换替换和$ afb_replacement)。
我想知道的是:
这又如何更有效地写的?也许在一行代码中。我相信有更好的办法,因为这是多余的代码,但目前还没有其他解决方案进入我的脑海。有任何想法吗?
我对你的方法很好奇!
如果你使用'compact()'和'extract()',它会保存一些变量名的输入。 – BoltClock 2011-05-15 19:47:47