d
Amit DhamuSoftware Engineer
 

Case Insensitive Multi Dimensional Array Sort

2 minute read 00000 views

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
        )

)