"数组到字符串转换错误"在laravel刀片文件中

时间:2016-06-08 02:45:57

标签: php laravel blade

我的laravel控制器中有一个$array_variable

{"11161":{"total":1,"1":1,"2":0,"3":0,"4":0},"11160":{"total":1,"1":1,"2":0,"3":0,"4":0},"11159":{"total":5,"1":5,"2":0,"3":0,"4":0},"11158":{"total":1,"1":1,"2":0,"3":0,"4":0},"11157":{"total":2,"1":2,"2":0,"3":0,"4":0},"11156":{"total":2,"1":2,"2":0,"3":0,"4":0},"11155":{"total":1,"1":1,"2":0,"3":0,"4":0},"11154":{"total":2,"1":2,"2":0,"3":0,"4":0},"11153":{"total":1,"1":1,"2":0,"3":0,"4":0},"11152":{"total":2,"1":2,"2":0,"3":0,"4":0},"11151":{"total":2,"1":2,"2":0,"3":0,"4":0},"11137":{"total":2,"1":2,"2":0,"3":0,"4":0},"11150":{"total":2,"1":2,"2":0,"3":0,"4":0},"11136":{"total":2,"1":2,"2":0,"3":0,"4":0},"11135":{"total":1,"1":1,"2":0,"3":0,"4":0},"11132":{"total":2,"1":2,"2":0,"3":0,"4":0},"11134":{"total":2,"1":2,"2":0,"3":0,"4":0},"11133":{"total":2,"1":2,"2":0,"3":0,"4":0},"11121":{"total":2,"1":2,"2":0,"3":0,"4":0},"11120":{"total":1,"1":1,"2":0,"3":0,"4":0},"11119":{"total":1,"1":0,"2":1,"3":0,"4":0}}

当我通过{{ $array_variable[$id] }}{{ $array_variable }}在刀片文件中访问它时,会出现此错误:

ErrorException
Array to string conversion

如何在没有错误的情况下访问数组元素?

3 个答案:

答案 0 :(得分:0)

它看起来不像一个数组,而是一个json对象..在任何一种情况下你都无法回应它。你可以做的是:json_decode()它然后在它上面的for / each循环来回显元素。

答案 1 :(得分:0)

当你得到$array_variable[$id]时,它会给你一个子查询:

"total" => [(...)]

不是字符串。这就是为什么你得到这个错误。 {{ (...) }}语句用于回显数据,当您尝试回显数组时,会出现此错误。你可以尝试这样的事情:

{{ 'total: ' . implode(', ', $array_variable[$id]['total']) }}

答案 2 :(得分:0)

使用var_dump打印数组

var_dump($array_variable[$id]);