<?php
$fields = [
    [
        "Tobacco:::Current every day smoker|Current some day smoker|Former smoker|Heavy tobacco smoker|Light tobacco smoker|Never smoker|Smoker, current status unknown|Unknown if ever smoked ",
    ],
    [
        "Alcohol:::Do not drink|Drink daily|Frequently drink|Hx of Alcoholism|Occasional drink",
        "Drug Abuse:::IVDU|Illicit drug use|No illicit drug use",
    ],
    [
        "Cardiovascular:::Eat healthy meals|Regular exercise|Take daily aspirin",
        "Safety:::Household Smoke detector|Keep Firearms in home|Wear seatbelts",
    ],
    [
        "Sexual Activity:::Exposure to STI|Homosexual encounters|Not sexually active|Safe sex practices|Sexually active",
        "Birth Gender:::Male|Female|Undifferentiated",
    ]
];

$newContentData = [];

for ($i = 0; $i < count($fields); $i++) {
    for($j = 0; $j < count($fields[$i]); $j++) {
        $parts = explode(":::", $fields[$i][$j]);
        $head = 'custom';
        if(!empty($parts[0])) $head = $parts[0];
        $values = explode("|", $parts[1]);
        for($k = 0; $k < count($values); $k++) {
            $fName = $head . '_' . sanitize_field_name($values[$k]);
            $newContentData[$fName] = $request->get($fName);
        }
    }
}

$newContentData['comments'] = $request->get('comments');
