我想将数组转换为Laravel中的字符串。我已经搜索并实现了implode()
函数来转换它但得到了这个错误
ErrorException (E_NOTICE) Array to string conversion
以下是我在控制器中的代码
$sliderImageDataArray[] =array(
"title"=> $value->title,
"text"=> "<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">".$value->text."<\/span><\/p>\n<p><a class=\"btn btn-secondary\" href=\"http://localhost/sencare/book-appoinment/\" target=\"_self\"> Make An Appointment <\/a>\u00a0<a class=\"btn btn-light\" href=\"http://localhost/sencare/our-doctors/\" target=\"_self\"> Our Doctors<\/a><\/p>\n",
"is_video"=> false,
);
$sliderImageDataArray = implode(" ",$sliderImageDataArray);
return $sliderImageDataArray;
请有人帮忙
答案 0 :(得分:0)
请注意[]
之后的$sliderImageDataArray
。您实际上是将具有这些值的数组分配给$sliderImageDataArray
数组的第一个元素。所以,implode()
实际上尝试通过该数组加入,而不是内部加入。由于生成的部分是数组而不是字符串,因此会出现Array to string conversion
错误。删除[]
:
<?php
$sliderImageDataArray =array(
"title"=> "test title",
"text"=> "<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">value<\/span><\/p>\n<p><a class=\"btn btn-secondary\" href=\"http://localhost/sencare/book-appoinment/\" target=\"_self\"> Make An Appointment <\/a>\u00a0<a class=\"btn btn-light\" href=\"http://localhost/sencare/our-doctors/\" target=\"_self\"> Our Doctors<\/a><\/p>\n",
"is_video"=> false,
);
$sliderImageDataArray = implode(" ",$sliderImageDataArray);
echo $sliderImageDataArray;
它运作正常。
答案 1 :(得分:0)
使用以下代码
{!! str_replace("'", "\'", json_encode($sliderImageDataArray)) !!};
而不是
$sliderImageDataArray = implode(" ",$sliderImageDataArray);