按字母顺序按值对数组进行排序 php [英] Sort array by value alphabetically php

查看:42
本文介绍了按字母顺序按值对数组进行排序 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我想在 php 中按字母顺序对数组进行排序.

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

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

会变成

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

有什么想法吗?

这是我要排序的实际数组.

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 )

推荐答案

你想要php函数asort":

You want the php function "asort":

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

它对数组进行排序,维护索引关联.

it sorts the array, maintaining the index associations.

我刚刚注意到您正在使用标准数组(非关联).如果您不介意保留索引关联,请使用 sort():

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

这篇关于按字母顺序按值对数组进行排序 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆