As the title suggests i want to sort an array by value alphabetically in php.
$arr = array(
\'k\' => \'pig\',
\'e\' => \'dog\'
)
sort()
. This will give a new array with numeric keys starting from 0
.asort()
.See also the comparison table of sorting functions in PHP.
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
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);
asort()
- Maintains key association: yes.
sort()
- Maintains key association: no.
Source: http://php.net/manual/en/array.sorting.php