Skip to content
Advertisement

Laravel foreach loop error message: Server Error in Controller

I pull the data in json format, but I can’t access the sub-elements in some way, it’s not oddly worth it, it only takes the first character.

$log_example = $request->all();
$logs = json_encode($log_example);
$data = json_decode((string) $logs, true);
return $data['log_list'];

The returned data is as follows

{
   "0":["1","SALES","5,00","REMOVED"],
   "1":["2","SALES","10,00","REMOVED"],
   "2":["1","BUYER","2","DROPPED"]
}

When I output with the dd function, the data returns as follows

dd($log_example);

return as below

array:1 [ "log_lists" => "{"0":["1"," SALES","10,00","REMOVED"],"1":["1"," SALES","10,00","REMOVED"],"2":["1"," BUYER","10,00","DROPPED"]}" ]

Here I want to pull the first data for example

foreach ($data as $d) {
       echo $d[0];
    }

return only this charecters {

Advertisement

Answer

The entry in the key value array is JSON, not the whole log_example, i would not decode it as you do, instead something like this.

$data = json_decode($log_example['log_lists'], true);

Now you should be able to loop as shown here.

foreach($data as $arr) {
    foreach($arr as $value) {
        var_dump($value)
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement