PHP:每5个单词后将单词从数组添加到字符串

PHP:每5个单词后将单词从数组添加到字符串,php,arrays,Php,Arrays,我有一个字符串,我想在每5个单词后面放一个单词。这个词来自数组 French History Maths Physics Spanish Chemistry Biology English DT Maths History DT Spanish English French RS 我想从数组中得到2个单词,然后在5个单词之后插入到上面的字符串中 $words = arrray(a, able, about, above, abst, accordance, according, accordi

我有一个字符串,我想在每5个单词后面放一个单词。这个词来自数组

French History Maths Physics Spanish Chemistry Biology English DT Maths History DT Spanish English French RS
我想从数组中得到2个单词,然后在5个单词之后插入到上面的字符串中

$words = arrray(a, able, about, above, abst, accordance, according, accordingly, across, act, actually, added, adj);
像这样的输出:

French History Maths Physics Spanish a able about above abst Chemistry Biology English DT Maths according according accordingly across act Spanish English French RS.
$string='法国历史数学物理西班牙化学生物英语DT数学历史DT西班牙英语法语RS';
$words=array('a'、'able'、'about'、'about'、'about'、'abs'、'concurrence'、'precurrence'、'cross'、'act'、'actual'、'added'、'adj');
$result=[];
$xpld=explode(“”,$string);
如果(!empty($xpld))
{
$count=0;
对于($i=0;$i0)
{
对于($j=$count;$j<$count+5;$j++)
{
$result[]=@$words[$j];
}
$count=$j;
}
$result[]=$xpld[$i];
}
}
$result=内爆(“”,$result);
回声$结果;


上面的代码与您的示例中的代码类似,但我不知道它是否有效,请注意,我使用了
suppress@
以避免数组中的单词脱离索引

您是否对此进行了任何尝试,请在问题中添加任何代码(非工作代码可以)。我已经进行了任何尝试。我不知道怎么开始。感谢您回答我的问题,但在您的示例中,每添加5个单词,您就从
数组中添加5个单词,而不是2个,否则我会误解?有一些资源至少是一个开始。@Joseph是的,但这只是我想展示的概念。很抱歉插入5个单词。非常感谢,这是我正在寻找的答案。
$string = 'French History Maths Physics Spanish Chemistry Biology English DT Maths History DT Spanish English French RS';
$words  = array('a', 'able', 'about', 'above', 'abst', 'accordance', 'according', 'accordingly', 'across', 'act', 'actually', 'added', 'adj');
$result = [];

$xpld = explode(' ',$string);
if(!empty($xpld))
{
  $count = 0;
  for ($i=0; $i < sizeof($xpld) ; $i++) 
  {
    if ($i % 5 == 0 && $i > 0) 
    {
      for ($j=$count; $j < $count + 5 ; $j++) 
      {
        $result[] = @$words[$j];
      }
      $count = $j;
    }
    $result[] = $xpld[$i];
  }
}
$result = implode(' ',$result);
echo $result;