[英]Sort array by value alphabetically php
As the title suggests i want to sort an array by value alphabetically in php.正如标题所示,我想在 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 )
You want the php function "asort": 你想要php函数“asort”:
http://php.net/manual/en/function.asort.php 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(): 如果您不想保留索引关联,请使用sort():
http://php.net/manual/en/function.sort.php 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);
sort()
. 如果您只想对数组值进行排序而不关心键,请使用sort()
。 This will give a new array with numeric keys starting from 0
. 这将给出一个新数组,其数字键从0
开始。 asort()
. 如果要保持键值关联,请使用asort()
。 See also the comparison table of sorting functions in PHP . 另请参见PHP中排序函数的比较表 。
asort()
- Maintains key association: yes . asort()
- 维持关键关联: 是的 。
sort()
- Maintains key association: no . sort()
- 维护关键关联: 否 。
Source: http://php.net/manual/en/array.sorting.php 资料来源: http : //php.net/manual/en/array.sorting.php
Using sort()
function, I have:使用sort()
function,我有:
$array = [
"Tannois",
"Bar-le-Duc",
"Val-d'Ornain",
"Fains-Véel",
"Érize-Saint-Dizier",
"Ville-sur-Saulx",
"Seigneulles",
"Behonne"
];
sort($array);
Result is:结果是:
[
"Bar-le-Duc",
"Behonne",
"Fains-Véel",
"Seigneulles",
"Tannois",
"Val-d'Ornain",
"Ville-sur-Saulx",
"Érize-Saint-Dizier"
]
Érize-Saint-Dizier
is the last. Érize-Saint-Dizier
是最后一个。
How I can sort it like:我如何排序:
[
"Bar-le-Duc",
"Behonne",
"Érize-Saint-Dizier",
"Fains-Véel",
"Seigneulles",
"Tannois",
"Val-d'Ornain",
"Ville-sur-Saulx"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.