Laravel:如何在转义控制器文本中打印出数组中的字符串



我正在设置一个文本块以发送电子邮件并设置$body变量。 问题是我的$app->reference_image_url字段是一个字符串数组,所以我得到一个array to string conversion错误。 我尝试了一个 foreach 循环来添加每个 url 字符串,但这引发了一个错误。

$body = <<<COMMENT
* Main Image
$app->image_url
$app->image_notes
* Reference Images
$app->reference_image_url
$app->reference_image_notes
COMMENT;
$this->send($body);

如果您希望结果格式化为字符串(可能带有逗号(,那么您可以使用 php 内爆函数。 https://www.php.net/manual/en/function.implode.php

为了方便您参考,我在此处附上了一个从手册中复制的代码块:

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone

最新更新