Amit Dhamu

Writes Code

Case Insensitive Multi Dimensional Array Sort

Array

$array[1]['name'] = 'Big';
$array[2]['name'] = 'alphabet';
$array[3]['name'] = 'zebra';
$array[4]['name'] = 'Dog';
$array[5]['name'] = 'mouse';
$array[6]['name'] = 'Cherry';
$array[7]['name'] = 'Tangerine';

Sort

usort($array, function($a, $b) {
    return strnatcasecmp($a['name'], $b['name']);
});

Output

Array
(
    [0] => Array
        (
            [name] => alphabet
        )

    [1] => Array
        (
            [name] => Big
        )

    [2] => Array
        (
            [name] => Cherry
        )

    [3] => Array
        (
            [name] => Dog
        )

    [4] => Array
        (
            [name] => mouse
        )

    [5] => Array
        (
            [name] => Tangerine
        )

    [6] => Array
        (
            [name] => zebra
        )

)


php / sort / strnatcasecmp / usort

← Is Element Visible In Viewport Step Through Commits On A Branch In Git →