$query = "
SELECT
order_id,
SUM(total) as total
FROM orders
WHERE user_id = '$user_id'
GROUP BY order_id
;"
$orders = [];
while($row = mysqli_fetch_assoc($res)) {
$orderId = $row['order_id'];
unset($row['order_id']);
$orders[$orderId]['id'] = $orderId;
$orders[$orderId]['products'][] = $row;
}