我出现了这个错误:
htmlspecialchars() expects parameter 1 to be string, object given
我在控制器中使用:
$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}'
$newData = json_decode($data);
我把它作为数组发送到视图:'data' => $newData 当我试图在视图中使用$data时,它给我的错误是
我已经尝试过使用$data->ac或者$data['ac'],但还是一样。 请给我一些帮助?
当你使用刀片echo{{$data }}
时,它将自动转义输出。它只能转义字符串。在你的数据中,$data->ac
是一个数组,$data
是一个对象,它们都不能按原样被回显。你需要更具体地说明数据应该如何被输出。具体是什么样子的,完全取决于你要完成的任务。例如,为了显示链接,你需要做{{$data->ac[0][0]['url'] }}
(不知道为什么你有两个嵌套数组,但我只是按照你的数据结构来做)。
@foreach($data->ac['0'] as $link)
<a href="{{ $link['url'] }}">This is a link</a>
@endforeach
如果你的目的是将完整的数组从html发送到控制器,可以使用这个:
从Blade.php中:
<input type="hidden" name="quotation" value="{{ json_encode($quotation,TRUE)}}">
在控制器中
public function Get(Request $req) {
$quotation = array('quotation' => json_decode($req->quotation));
//or
return view('quotation')->with('quotation',json_decode($req->quotation))
}
你可以使用`serialize
<input type="hidden" name="quotation[]" value="{{serialize($quotation)}}">
但在这种情况下,最好的办法是在你的刀片中使用json_encode
方法,在控制器中使用json_decode
。