Sort Multidimensional Array by Value

$list = Array
(
    [0] => Array
        (
            [lat] => '43.65529'
            [lng] => '-79.38397'
            [name] => 'Name'
        )
    [1] => Array
        (
            [lat] => '43.65328'
            [lng] => '-79.37947'
            [name] => 'Name'
        )
    [2] => Array
        (
            [lat] => '43.65516'
            [lng] => '-79.38892'
            [name] => 'Name'
        )
)

Sort with latitude going smallest to largest with longitude also going smallest to largest after

array_multisort(
	array_column($list, 'lat'), SORT_ASC, 
	array_column($list, 'lng'), SORT_ASC,
	$row
);

Array now looks like this

Array
(
    [0] => Array
        (
            [lat] => '43.65328'
            [lng] => '-79.37947'
            [name] => 'Name'
        )
    [1] => Array
        (
            [lat] => '43.65516'
            [lng] => '-79.38892'
            [name] => 'Name'
        )
    [2] => Array
        (
            [lat] => '43.65529'
            [lng] => '-79.38397'
            [name] => 'Name'
        )
)

Leave a Reply

Your email address will not be published. Required fields are marked *