Grouping array entries based on a particular key

Hello, I'm a beginner and I can't wrap my head around this.
I'm expecting the input from the frontend to be something like this:

[
 {"order_id":1001,
  "line_items":[
     {"id":1,
     "quantity":1
     }]
 },
 {"order_id":1002,
  "line_items":[
     {"id":2,
     "quantity":1
     }]
 },
 {"order_id":1002,
  "line_items":[
     {"id":3,
     "quantity":1
     }]
 }
]

I want to transform it to this:

[
 {"order_id":1001,
  "line_items":[
     {"id":1,
     "quantity":1
     }]
 },
 {"order_id":1002,
  "line_items":[
     {"id":2,
     "quantity":1
     },
     {"id":3,
     "quantity":1
     }]
 }
]

Basically grouping line_items under the same order_id.

Any beginner friendly ideas? ๐Ÿ˜€
Thanks,

Nancy

1
5 replies