Search code examples
phparraysjsonphp-8laravel-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 = ["16502735051.jpg","16502735052.jpg","16502735053.jpg","16502735055.jpg"];

Now I decoded this JSON into an Array like below:-

$arr = json_decode($json);

when I print this arr using print_r it returns as below:-

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


Solution

  • 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. That's why other rows were returning null this Laravel returned Fatal error and stopped the script.