如何更改laravel db查询构建器括号

时间:2020-06-06 18:01:20

标签: mysql laravel laravel-5 post query-builder

需要问, 目前,我正在使用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
    }
]

我需要将括号[]更改为{}。 该怎么做?

谢谢

1 个答案:

答案 0 :(得分:0)

您已经更新了问题,所以我更新了答案:

$remove = array("[", "]");
$replace   = array("{", "}");
$newJson = str_replace($remov, $replace, $oldJson);