Alphabetically sort a flat associative array by values and preserve the original keys
Asked Answered
F

4

50

I want to sort a flat, associative array by its values alphabetically and preserve the original keys.

$arr = [
    0 => 'Newtown',
    1 => 'Montgomery',
    2 => 'Welshpool',
    6 => 'Llanfyllin',
    7 => 'Llansanffraid',
    8 => 'Llanymynech',
    9 => 'Oswestry',
    14 => 'Oswestry Town Service',
];

would become

[
    6 => 'Llanfyllin',
    7 => 'Llansanffraid',
    8 => 'Llanymynech',
    1 => 'Montgomery',
    0 => 'Newtown',
    9 => 'Oswestry',
    14 => 'Oswestry Town Service',
    2 => 'Welshpool',
]

Any ideas?

Here's my full, actual array to sort:

$arr = [
    0 => 'Newtown',
    1 => 'Montgomery',
    2 => 'Welshpool',
    6 => 'Llanfyllin',
    7 => 'Llansanffraid',
    8 => 'Llanymynech',
    9 => 'Oswestry',
    14 => 'Oswestry Town Service',
    15 => 'Aston Way',
    16 => 'College Road',
    17 => 'Shrewsbury',
    18 => 'Royal Shrewsbury Hospital',
    19 => 'Worthen',
    20 => 'Brockton',
    22 => 'Cefn Blodwell',
    23 => 'Treflach',
    24 => 'Trefonen',
    25 => 'Morda',
    26 => 'Marches School',
    28 => 'North Shropshire College',
    37 => 'Park Hall',
    38 => 'Gobowen',
    39 => 'St Martins',
    40 => 'Ifton Heath',
    42 => 'Guilsfield',
    43 => 'Four Crosses',
    45 => 'Pant',
    46 => 'Llynclys',
    49 => 'Oswestry Town Service Schools',
    51 => 'Woodside School',
    56 => 'Whittington',
    57 => 'Babbinswood',
    58 => 'Hindford',
    59 => 'Ellesmere',
    62 => 'Forden',
    63 => 'Kingswood Cock Hotel',
    65 => 'Coleg Powys',
    85 => 'Borfa Green',
    86 => 'Bryn Siriol',
    87 => 'Maesydre School',
    92 => 'Crew Green',
    93 => 'Ford',
    104 => 'Llanrhaeadr',
    106 => 'Meifod',
    114 => 'Llangynog',
    116 => 'Llangedwyn',
    119 => 'Porthywaen',
    132 => 'Llanfair Caereinion',
    133 => 'Pontrobet',
    136 => 'Dolanog',
    141 => 'Llansilin',
    144 => 'Abermule',
    145 => 'Llandyssil',
    146 => 'Carhowel',
    149 => 'Cefn Coch',
    150 => 'Tregynon',
    151 => 'Manafon',
    152 => 'Berriew',
    157 => 'Bettws Cedewain',
    158 => 'Newtown High School',
    160 => 'Newtown Coleg Powys',
    173 => 'Llanerfyl',
    174 => 'Machynlleth',
    175 => 'Talybont',
    176 => 'Aberystwyth',
    183 => 'Bala',
    184 => 'Llanrwst',
    185 => 'Llandudno',
    188 => 'Middletown',
    196 => 'Llanidloes',
    202 => 'Wrexham',
    203 => 'Rhayader',
];
    
Fosque answered 4/11, 2009 at 11:37 Comment(0)
E
55

You want the php function "asort":

http://php.net/manual/en/function.asort.php

it sorts the array, maintaining the index associations.

Edit: I've just noticed you're using a standard array (non-associative). if you're not fussed about preserving index associations, use sort():

http://php.net/manual/en/function.sort.php

Ellington answered 4/11, 2009 at 11:40 Comment(0)
B
28

Note that sort() operates on the array in place, so you only need to call

sort($a);
doSomething($a);

This will not work;

$a = sort($a);
doSomething($a);
Brice answered 4/11, 2009 at 11:56 Comment(2)
It is because sort($a) passes the value by reference because of that we don't need to assign it to the second variable.Promiscuity
This answer does not preserve the original keys.Inspired
G
9
  • If you just want to sort the array values and don't care for the keys, use sort(). This will give a new array with numeric keys starting from 0.
  • If you want to keep the key-value associations, use asort().

See also the comparison table of sorting functions in PHP.

Godewyn answered 4/11, 2009 at 11:40 Comment(0)
U
2

asort() - Maintains key association: yes.

sort() - Maintains key association: no.

Source: http://php.net/manual/en/array.sorting.php

Unconformable answered 21/12, 2017 at 17:13 Comment(1)
Does this late answer actually add any new knowledge to the page?Inspired

© 2022 - 2024 — McMap. All rights reserved.