php - passing json array form in the put storage method in laravel php
问题描述
iam trying to pass $data array to save it in the laravel storage files my code is :
$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';
i want to change the values inside each year according to database values , the problem is that when iam trying to put method iam getting the following error :
Array to string conversion
if i pass $data array without doing json_decode it works fine , but in this case i cant change the data isnide the array , so i have to decode it .
解决方案
在这种情况下,您应该使用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);
推荐阅读
- ios - 移动 Safari 中的 React PWA 图像上传会中断应用程序吗?
- scala - 在我用 ^Z (在 linux 上)暂停它后,scala repl 失去了 TAB 自动完成功能。我可以自动完成吗?
- xamarin.forms - 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例
- c# - WPF ShowDialog() 的 Blazor 等价物?
- ruby - Capybara / Ruby / Cucumber - 使命令期望在证据中突出显示
- c# - 如何在注销或无效用户凭据时删除身份 cookie
- delphi - Delphi ActionMainMenuBar 类别位置
- excel - 两次关闭工作簿时的奇怪行为
- postgresql - docker-compose postgres 卷将所有权更改为 gitlab-runner
- django - How to initialize class properties in inherited models