简体   繁体   English

Laravel- json_decode 返回数组但无法回显 Laravel 9 中的数组值

[英]Laravel- json_decode returns array but cannot echo array values in Laravel 9

So I have a Json data in my Database like below:-所以我的数据库中有一个 Json 数据,如下所示:-

$json = ["16502735051.jpg","16502735052.jpg","16502735053.jpg","16502735055.jpg"];

Now I decoded this JSON into an Array like below:-现在我将这个 JSON 解码成如下所示的数组:-

$arr = json_decode($json);

when I print this arr using print_r it returns as below:-当我使用 print_r 打印此 arr 时,它返回如下:-

Array ( [0] => 16502735051.jpg [1] => 16502735052.jpg [2] => 16502735053.jpg [3] => 16502735054.jpg [4] => 16502735055.jpg ) 

The prob occurs when I try to get the value from the array.当我尝试从数组中获取值时会发生此问题。 I write this:-我这样写:-

echo $arr[0]

It return error.它返回错误。 Shows:- Trying to access array offset on value of type null显示:-尝试访问 null 类型值的数组偏移量

I got the answer.我得到了答案。 It's not a bug or something.这不是一个错误或什么的。 The problem was happening because, I have three rows in the DB table.发生问题是因为我在数据库表中有三行。 Only one of them has Json Value.其中只有一个具有 Json 值。 That's why other rows were returning null this Laravel returned Fatal error and stopped the script.这就是为什么其他行返回null这个 Laravel 返回致命错误并停止脚本的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM