如何删除laravel db查询生成器括号

问题描述 投票:0回答:1

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

我需要移除括号[],仅结果数组。怎么做?

之前感谢

mysql laravel laravel-5 post query-builder
1个回答
0
投票

您可以使用trim功能:

$data = trim($result, '[]');

也可以使用str_replace功能:

$data = str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));
© www.soinside.com 2019 - 2024. All rights reserved.