使用 str_replace 时出现多数组错误

Multiple array error when using str_replace

我想替换如下文字。

$text = '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<pre><code>[[EXAMPLE]] this is a text. <br />[[EXAMPLE2]]</code></pre>
<p style="text-align: center;"><br /><br /></p>
</body>
</html>';


$products['type'] = 1;
$products['quantity'] = 2300.0;
$products['grade'] = null;

 $searchVal = array(
'[[EXAMPLE]]'
'[[EXAMPLE2]]'
);

 $replaceVal = array(
'replace word',
 $products
); 

$text = str_replace($searchVal, $replaceVal, $text);

我没有运行这个代码。这次失败是 "Array to string conversion"

我该怎么做。

根据 documentation:

If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject

确保 $searchVal$replaceVal 都是线性数组,目前它们不是。

您的 $searchVal 定义缺少逗号。

您的 $replaceVal 是一个数组,其中第二个元素是另一个数组。