<?php

/**
 * Detects the field delimiter (';' or ',') for a CSV file.
 * It bases the detection on the character count in the header row.
 */
function detectSeparators(string $csvContent): array
{
    $lines = preg_split('/\r\n|\n|\r/', trim($csvContent));
    $header = $lines[0] ?? '';

    $semicolonCount = substr_count($header, ';');
    $commaCount = substr_count($header, ',');

    // If comma count is higher, we assume comma is the delimiter.
    // Otherwise, we default to semicolon, which is common in many European locales.
    if ($commaCount > $semicolonCount) {
        $delimiter = ',';
    } else {
        $delimiter = ';';
    }

    // The decimal separator is no longer detected here, 
    // as it will be handled on a per-value basis.
    return [
        'delimiter' => $delimiter
    ];
}

/**
 * Recursively traverses an array and converts numeric strings to int or float.
 * Non-numeric strings are left unchanged.
 *
 * @param array $array The array to process, passed by reference.
 */
function recursively_convert_numeric_strings(array &$array)
{
    foreach ($array as &$value) {
        if (is_array($value)) {
            // If the value is an array, recurse into it
            recursively_convert_numeric_strings($value);
        } elseif (is_string($value) && is_numeric($value)) {
            // If the value is a string and represents a number, convert it
            // Check if it's an integer or a float
            if ((float)$value == (int)$value) {
                $value = (int)$value;
            } else {
                $value = (float)$value;
            }
        }
        // If it's not a string, not numeric, or not an array, it's left as is.
    }
}
