[php] Sort array by value alphabetically php

As the title suggests i want to sort an array by value alphabetically in php.

$arr = array(
    'k' => 'pig',
    'e' => 'dog'
)

would become

$arr = array(
    'e' => 'dog',
    'k' => 'pig'
)

Any ideas?

EDIT: Here's the actual array i want to sort.

Array ( [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 )

This question is related to php arrays sorting

The answer is


asort() - Maintains key association: yes.

sort() - Maintains key association: no.

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


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);

  • 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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to sorting

Sort Array of object by object field in Angular 6 Sorting a list with stream.sorted() in Java How to sort dates from Oldest to Newest in Excel? how to sort pandas dataframe from one column Reverse a comparator in Java 8 Find the unique values in a column and then sort them pandas groupby sort within groups pandas groupby sort descending order Efficiently sorting a numpy array in descending order? Swift: Sort array of objects alphabetically