laravel 尝试在数组上分配属性“RoundNumber”

dwbf0jvd  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(95)

我正尝试从一个json文件中提取RoundNumber数据,该文件是在我的laravel应用程序中通过请求传递的,方法如下:

public function importResults(Request $request)
{
    $json = file_get_contents($request->file('results'));
    $decoded = json_decode($json);

    $roundNumber = $decoded->data->MatchingTables->RoundNumber;

    return $roundNumber;
    //return redirect('getPanel');
}

这是我的json:

"MatchingTables": [{
            "RoundNumber": 1,
            "id_round": "JLLcrGFKokzyP147",
            "Team1": "xPn5sTmNcwZwA9XA",
            "Team2": null,
            "GameWins2": 0,
            "GameWins1": 0,
            "GameByes2": 0,
            "GameByes1": 0,
            "GameDraws": null,
            "Player1": null,
            "Player2": null,
            "Number": null,
            "Timestamp": 1676536691086,
            "MatchOrdinal": null,
            "_id": "5r14sENZ4LPyYp0Y"
        },
        {
            "Number": 3,
            "Timestamp": 1676536642961,
            "RoundNumber": 1,
            "Team1": "BtjUYtJKqB1DswuF",
            "Team2": "a0M2B44mfw6zgJPh",
            "Player1": "hFXO7VhvuB6dc9u4",
            "Player2": "yyCW3cTW3lbf3HP8",
            "GameWins1": null,
            "GameWins2": null,
            "GameByes1": null,
            "GameByes2": null,
            "GameDraws": null,
            "MatchOrdinal": 3,
            "id_round": "JLLcrGFKokzyP147",
            "_id": "7CgcJdgQJj3QpR7Z"
        },
        {
            "RoundNumber": 1,
            "id_round": "JLLcrGFKokzyP147",
            "Team1": "QtlYSLJ4RI9tUc09",
            "Team2": null,
            "GameWins2": null,
            "GameWins1": null,
            "GameByes2": null,
            "GameByes1": 1,
            "GameDraws": null,
            "Player1": null,
            "Player2": null,
            "Number": null,
            "Timestamp": 1676536688524,
            "MatchOrdinal": null,
            "_id": "PX8gLrgQ38RmOCUO"
        },
        {
            "Number": 1,
            "Timestamp": 1676536642961,
            "RoundNumber": 1,
            "Team1": "ZbBlDh1Uyt3e8Uvf",
            "Team2": "is01LkEzsWDtLTPv",
            "Player1": "62da1vAAK6z8bbxR",
            "Player2": "HPT4CJ8YM98WoqX3",
            "GameWins1": null,
            "GameWins2": null,
            "GameByes1": null,
            "GameByes2": null,
            "GameDraws": null,
            "MatchOrdinal": 1,
            "id_round": "JLLcrGFKokzyP147",
            "_id": "QWs3NXYofMS2dcKZ"
        },
        {
            "Number": null,
            "Timestamp": 1676536642961,
            "RoundNumber": 1,
            "Team1": "92Wlqmulw5YdtATz",
            "Player1": "iuv4J5dIlSo7LMfX",
            "Player2": null,
            "GameWins1": null,
            "GameWins2": null,
            "GameByes1": 1,
            "GameByes2": null,
            "GameDraws": null,
            "MatchOrdinal": 5,
            "id_round": "JLLcrGFKokzyP147",
            "_id": "eEEBX2dg5c8V5E1z"
        },
        {
            "Number": 2,
            "Timestamp": 1676536642961,
            "RoundNumber": 1,
            "Team1": "b4nenH6nO8mk2fr0",
            "Team2": "S65ushxkZAF53bZn",
            "Player1": "DaFDCZdVNWkizy6N",
            "Player2": "SFamW9qeVlVvoAq4",
            "GameWins1": null,
            "GameWins2": null,
            "GameByes1": null,
            "GameByes2": null,
            "GameDraws": null,
            "MatchOrdinal": 2,
            "id_round": "JLLcrGFKokzyP147",
            "_id": "oiXAZVsV5zevpPvi"
        }],

我想从数组的每个元素中提取“RoundNumber”属性。我也尝试了以下方法:

$roundNumber = $decoded->data->MatchingTables["RoundNumber"];
$roundNumber = $decoded->data->MatchingTables[]->RoundNumber;
b5lpy0ml

b5lpy0ml1#

你不能像这样在数组中提取数据,你要么指定你想要从数组中提取的键,要么在获取RoundNumber值之前通过循环或使用laravel集合来获取所有的值。
e.i您可以使用collection和pluck
这将为您提供一个值数组,您传递的键

return collect( $decoded->data->MatchingTables)->pluck('RoundNumber');

相关问题