我有一个多维数组,我试图根据重复值对数组进行分组。因此,我尝试按键对它们进行分组,但我无法根据键值对数组进行分组。B...
我有一个多维数组,我正在尝试根据重复值对该数组进行分组。
因此,我尝试按键对它们进行分组,但我无法根据键值对数组进行分组。
以下是原始数组
[
{
"league_id": 2,
"league_name": "BBBB",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1722361078.png",
"order_number": 1,
"member_username": "A"
},
{
"league_id": 2,
"league_name": "BBBB",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1722361078.png",
"order_number": 2,
"member_username": "B"
},
{
"league_id": 1,
"league_name": "AAAA",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1724177635.png",
"order_number": 1,
"member_username": "C"
},
{
"league_id": 1,
"league_name": "AAAA",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1724177635.png",
"order_number": 2,
"member_username": "D"
},
]
我想将该数组重新排列为下面的输出数组:
[
{
"league_id": 2,
"league_name": "BBBB",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1722361078.png",
"users": [{
"order_number": 1,
"member_username": "A"
}, {
"order_number": 2,
"member_username": "B"
}]
},
{
"league_id": 1,
"league_name": "AAAA",
"league_image": "https://d2ayu7u0r0spqb.cloudfront.net/dev/league-images/1722361078.png",
"users": [{
"order_number": 1,
"member_username": "C"
}, {
"order_number": 2,
"member_username": "D"
}]
}
]
有什么想法吗?如何做到这一点。
谢谢