打开基于逗号分隔数组的新选项卡


Open new tab based on comma-seperated array

首先,如果问题标题有点混乱,很抱歉。这是我提出这个问题的最好方式。

主要的问题是,我有一个PHP脚本如下:

$variable = '1,12,16';
$myArray = explode(',', $variable);
foreach($myArray as $my_Array){ 
echo "<script type='"text/javascript'">
window.open('http://example.com/pages/"$my_Array".html', '_blank')</script>";
}

这段代码应该产生以下输出:

Open 3 new tabs
1st tab: 1.html
2nd tab: 12.html
3rd tab: 16.html

如有任何帮助,我们将不胜感激!!

如果有人为这个问题建议一个更好的标题/描述,我也会很高兴。

在代码中尝试此更改

echo "<script type='"text/javascript'">
  window.open('http://example.com/pages/".$my_Array.".html', '_blank') 
</script>"; //missed concatenation in $myarray variable

问题是没有正确连接字符串。以下是您应该做的:

$variable = '1,12,16';
$myArray = explode(',', $variable);
foreach($myArray as $my_Array){ 
echo "<script type='"text/javascript'">
window.open('http://example.com/pages/".$my_Array.".html', '_blank')</script>";
}