【问题标题】:PHP foreach iterate over the previous resultPHP foreach 迭代上一个结果
【发布时间】:2015-10-07 09:16:20
【问题描述】:

我有一个要插入到文本中的字符串数组。

$text "some text";
foreach ($arrayStrings as $string){
 $new_text = preg_replace($old_string, $new_string, $text);
}

如何设置它,以便每次迭代都使用 $new_text 的先前结果? IE。最后,我得到一个文本版本,其中包含数组中的所有新字符串,而不仅仅是最后一个,这就是我现在得到的。

谢谢

【问题讨论】:

  • $new_text -> $new_text[] 创建一个数组?!
  • 为什么这个问题被否决了?
  • 因为没有意义。您迭代一个数组,但您不使用数组元素 ($string)。

标签: php arrays loops foreach preg-replace


【解决方案1】:

我愿意:

$text "some text";
$new_text = $text;
foreach ($arrayStrings as $string){
    $new_text = preg_replace($old_string, $new_string, $new_text);
}

【讨论】:

  • $string 甚至没有被使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2012-06-30
  • 1970-01-01
相关资源
最近更新 更多