我有以下结构:
$text_1 = $this->getValue('value_1');
$text_2 = $this->getValue('value_2');
$text_3 = $this->getValue('value_3')
Run Code Online (Sandbox Code Playgroud)
还有以下内容:
foreach($text_1 as $t_1)
{
if(!$first)
{
$string_1 .= ",";
}
$first = false;
$string_1 .= $t_1;
}
foreach($text_2 as $t_2)
{
if(!$first)
{
$string_2 .= ",";
}
$first = false;
$string_2 .= $t_2;
}
foreach($text_3 as $t_3)
{
if(!$first)
{
$string_3 .= ",";
}
$first = false;
$string_3 .= $t_3;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以重新考虑使用计数器,比如在for循环中,从我的代码中替换_1,_2,_3等?
好吧,foreach循环可以替换为implode.
除此之外,为$text_??变量使用数组不是更好吗?例如.:
$text = $this->getValue('value');
foreach ($text as $value) {
$strings[] = implode(",", $value);
}
Run Code Online (Sandbox Code Playgroud)
很难给出更具体的建议,没有确切的背景,但这应该让你朝着正确的方向前进.