我试图传递$ data数组以将其保存在laravel存储文件中 我的代码是:
$data = '[
{
"label": " Total Accidents",
"color": "#5ab1ef",
"data":
[
["Jan",0],
["Feb",0],
["Mar",0],
["Apr",0],
["May",0],
["Jun",0],
["Jul",0],
["Aug",0],
["Sep",0]
]
},
{
"label": " Active Experts",
"color": "#f5994e",
"data":
[
["Jan",0],
["Feb",0],
["Mar",0],
["Apr",0],
["May",0],
["Jun",0],
["Jul",0],
["Aug",0],
["Sep",0]
]
},
{
"label": " Pending Payments",
"color": "#d87a80",
"data":
[
["Jan",0],
["Feb",0],
["Mar",0],
["Apr",0],
["May",0],
["Jun",0],
["Jul",0],
["Aug",0],
["Sep",0]
]
}
]';
$test = json_decode($data, true);
$test[0]['data'][1] = ["Jan", 153];
Storage::disk('chartData')
->put('chart/test'.$_SESSION['companyId'].'.json', $test);
return 'done';
我想根据数据库值更改每年内的值,问题是当iam尝试将方法iam收到以下错误时:
数组到字符串转换
如果我在没有执行json_decode的情况下传递$ data数组它工作正常,但在这种情况下我不能更改数据是否在数组中,所以我必须解码它。
答案 0 :(得分:3)
在这种情况下,您应该使用json_encode
函数将JSON数据转换为字符串。
$test[0]['data'][1] = ["Jan", 153];
$test = json_encode($test);// convert to string
Storage::disk('chartData')
->put('chart/test'.$_SESSION['companyId'].'.json', $test);