需要询问,目前,我正在使用laravel和数据库查询生成器来创建以下功能。要求是获取不带括号/对象但带有{}
的查询结果功能在下面:
$score = DB::table('m_player_scorecard')
->where('session_id', '=', $sessionID)
->orderBy('created_at', 'desc')
->get();
$scoreArr = json_decode(json_encode($score), true);
return response()
->json($scoreArr);
当我尝试从邮递员处打人时:
[
{
"id": 153,
"session_id": "9e3bb8296fd195c66ced470ec50301385f6cd31a17012e26fd0099bdf821338c",
"round_order": 2,
"player_one_id": 1,
"score_one": 10,
"player_two_id": 2,
"score_two": 10,
"player_three_id": 3,
"score_three": 30,
"player_four_id": 4,
"score_four": 40,
"notes": null,
"created_at": "2020-06-06 18:15:27",
"updated_at": null
},
{
"id": 152,
"session_id": "9e3bb8296fd195c66ced470ec50301385f6cd31a17012e26fd0099bdf821338c",
"round_order": 1,
"player_one_id": 1,
"score_one": 10,
"player_two_id": 2,
"score_two": 20,
"player_three_id": 3,
"score_three": 30,
"player_four_id": 4,
"score_four": 40,
"notes": null,
"created_at": "2020-06-06 18:14:39",
"updated_at": null
}
]
我需要将括号[]更改为{}。怎么做?
之前感谢
您已经更新了您的问题,所以我更新了我的答案:
$remove = array("[", "]");
$replace = array("{", "}");
$newJson = str_replace($remov, $replace, $oldJson);