Split and sort data

Split the alphabet using the regular expression and using usort() function

function splitandSort($csv) {

    foreach($csv  as $row) {
        if (preg_match("/([A-Z]){2}/i", $row['2'])) {
            $grp2[] = $row;
        } else {
            $grp1[] = $row;
        }
    }
    usort( $grp1,  'sortData'  );
    usort( $grp2, 'sortData'  );
    $result = array_merge($grp1,$grp2);
    return $result;
}

function sortData($a, $b) {
    return strnatcmp($a['2'], $b['2']);
}

Share this Tutorial