I am trying to restructure an array. Instead of variant_id at the top, i would like to make collection_id to the top. if the collection_id is the same they should be in the same array. I try using foreach but seems didn't work.
[
{
variant_id: '39264031277264',
collection: [
{
collection_id: '260687954128',
value: true
}
]
},
{
variant_id: '39264022134992',
collection: [
{
collection_id: '260687954128',
value: true
}
]
},
{
variant_id: '40460795642064',
collection: [
{
collection_id: '277033681104',
value: true
}
]
}
]
This is the expected result
[
{
"collection_id":"260687954128",
"variant_id":[
"39264031277264",
"39264022134992"
]
},
{
"collection_id":"277033681104",
"variant_id":[
"40460795642064"
]
}
]
{variant_id: '39264031277264', variant_id: '39264022134992'}.