How can PHP be used to sort data from a VARCHAR column by numbers only, disregarding alphabetical characters?

To sort data from a VARCHAR column by numbers only, disregarding alphabetical characters, we can use a custom sorting function in PHP. This function will extract the numerical value from each string and use it for sorting. By comparing the numerical values instead of the entire string, we can achieve the desired sorting result.

// Custom sorting function to extract numbers and compare them for sorting
function numericSort($a, $b) {
    preg_match_all('!\d+!', $a, $numsA);
    preg_match_all('!\d+!', $b, $numsB);

    $numA = implode('', $numsA[0]);
    $numB = implode('', $numsB[0]);

    return $numA - $numB;
}

// Sample data from a VARCHAR column
$data = ['abc123', 'def456', 'ghi789', 'jkl10'];

// Sort the data using the custom numericSort function
usort($data, 'numericSort');

// Output the sorted data
print_r($data);